##### Replication File: Journal of Law and Courts
##### Christine C. Bird, Jonathan M. King, and Zachary A. McGee
##### The Influence of Federalist Society Affiliation on Senator Voting in Federal Judicial Nominations
##### For questions on the data or analyses, please contact Jonathan M. King (jonathan.king@uga.edu)



#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#
#Data Set Up
#
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################

library(readr)
library(arm)
library(fastDummies)
library(stargazer)
library(fastDummies)
#Load Data
#BirdKingMcGee_JLC2025_ReplicationFiles <- read_csv("~/BirdKingMcGee_JLC2025_ReplicationFiles.csv")
noms_reppres=BirdKingMcGee_JLC2025_ReplicationFiles

noms_reppres_post2000=subset(noms_reppres,noms_reppres$Congress>=107)

noms_reppres_trump=subset(noms_reppres, noms_reppres$Congress>=115)

noms_pretrump=subset(noms_reppres,noms_reppres$Congress<115)

#Rep Pres Data
data.x.reppres=subset(noms_reppres,select=c('Female',"ABANum",'EliteSchool','SenParty','FedSocAffilJudge','SenatorUniqueID','Congress','courtlevelDC','SenatorDWNominate','SenHomeStateSen',
                                            'JCS'))
data.x.reppres=na.omit(data.x.reppres) #get rid of any NAs

#add dummies for senator unique ID
data.new.reppres=dummy_cols(data.x.reppres,select_columns = c('SenatorUniqueID','Congress','courtlevelDC'))


#set up subsets per court level
data.scotus.reppres=subset(data.new.reppres,data.new.reppres$courtlevelDC==1)
data.coa.reppres=subset(data.new.reppres,data.new.reppres$courtlevelDC==2)
data.district.reppres=subset(data.new.reppres,data.new.reppres$courtlevelDC==4)



#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#
#Models
#
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################


#### Appendix Models -- Contact for Full Data ######
#noms$AlliedSen=NA
#noms$AlliedSen=ifelse(noms$PresParty==1 & noms$SenParty==1,1,0)
#noms$AlliedSen=ifelse(noms$PresParty==0 & noms$SenParty==0,1,noms$AlliedSen)

#interaction model
#mlm.vote.interaction=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+AlliedSen+ #senator-vote covariates
#                             SenParty*FedSocAffilJudge+
#                             (1|SenatorUniqueID)+ #Varying intercept by Senator
#                             (1|Congress)+ #Varying intercept by Congress
#                             (1|courtlevelDC), #varying intercept by courtlevel
#                           family=binomial(link='logit'),
#                           data=noms)
#summary(mlm.vote.interaction) 



### interaction 2 (Nominate*FedSocJudge)
#mlm.vote.interaction2=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+AlliedSen+ #senator-vote covariates
#                              SenatorDWNominate*FedSocAffilJudge+
#                              (1|SenatorUniqueID)+ #Varying intercept by Senator
#                              (1|Congress)+ #Varying intercept by Congress
#                              (1|courtlevelDC), #varying intercept by courtlevel
#                            family=binomial(link='logit'),
#                            data=noms)
#summary(mlm.vote.interaction2)



  
###### Paper Models ###### 

#interaction 3 (Nominate*FedSocJudge, Rep only nominees)
mlm.vote.interaction3=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+ #senator-vote covariates
                              SenatorDWNominate*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_reppres)
summary(mlm.vote.interaction3)


#interaction 3.dime (Nominate*FedSocJudge, Rep only nominees; DIME)
mlm.vote.interaction3.dime=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+dime_cf_imputed+ #senator-vote covariates
                              SenatorDWNominate*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_reppres)
summary(mlm.vote.interaction3.dime)



#interaction 4 (Party*FedSocJudge, Rep only nominees)
mlm.vote.interaction4=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+ #senator-vote covariates
                              SenParty*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_reppres)
summary(mlm.vote.interaction4)

#interaction 4.dime (Party*FedSocJudge, Rep only nominees; DIME)
mlm.vote.interaction4.dime=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+dime_cf_imputed+ #senator-vote covariates
                              SenParty*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_reppres)
summary(mlm.vote.interaction4.dime)



### interaction/ robustness just looking at trump noms -- partyID
mlm.vote.interaction5=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+ #senator-vote covariates
                              SenParty*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_reppres_trump)
summary(mlm.vote.interaction5)


### interaction/ robustness just looking at trump noms -- nominate
mlm.vote.interaction6=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+ #senator-vote covariates
                              SenatorDWNominate*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_reppres_trump)
summary(mlm.vote.interaction6)


### robustness check pre-trump -- partyID
mlm.vote.interaction7=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+ #senator-vote covariates
                              SenParty*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_pretrump)
summary(mlm.vote.interaction7)

### robustness check pre-trump -- nominate
mlm.vote.interaction8=glmer(as.numeric(FinalVote)~Female+ABANum+EliteSchool+SenParty+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+JCS+ #senator-vote covariates
                              SenatorDWNominate*FedSocAffilJudge+
                              (1|SenatorUniqueID)+ #Varying intercept by Senator
                              (1|Congress)+ #Varying intercept by Congress
                              (1|courtlevelDC), #varying intercept by courtlevel
                            family=binomial(link='logit'),
                            data=noms_pretrump)
summary(mlm.vote.interaction8)


#Read In Data for noms_republican
#noms_republican=<- read_csv("~/BirdKingMcGee_noms_republican.csv")
#interaction (Republican Senators, FedSocSens*FedSocJudges)
mlm.vote.republican.reppres.int=glmer(FinalVote~Female+ABANum+EliteSchool+FedSocAffilJudge+SenatorDWNominate+SenHomeStateSen+FedSocSenator+JCS+
                                        FedSocAffilJudge*FedSocSenator+
                                        (1|SenatorUniqueID)+
                                        (1|Congress)+
                                        (1|courtlevelDC),
                                      family=binomial(link='logit'),
                                      data=noms_republican)
summary(mlm.vote.republican.reppres.int)



#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#
#Model Outputs
#
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################

### In-Text Model Outputs
#Table 1 (Interaction models)
stargazer(mlm.vote.interaction4,mlm.vote.interaction3,no.space=T)


### Appendix Model Outputs
#Table A1 (Full noms)
stargazer(mlm.vote.interaction, mlm.vote.interaction2, no.space=T)

#Table A (Republican Senator voting on FedSoc)
stargazer(mlm.vote.republican.reppres.int, no.space=T)

#Table A (pre/post Trump)
stargazer(mlm.vote.interaction8,mlm.vote.interaction5, no.space=T)








#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#
##### Simulations
#
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################


####################################################################################################################################
####################################################################################################################################
### Interactive Model Model Predictions (Party ID*FedSocNom) (Republican-Only Nominees)
n.draws=1000 #number of simulations
seq.vote=seq(from=0,to=1) #just to allow variation
sim.base.int4=coef(sim(mlm.vote.interaction4,n.draws))
sim.base.int4=as.data.frame(sim.base.int4)
####################################################################################################################################
####################################################################################################################################

######################################################
##################### SCOTUS #########################
######################################################

#set up matrices for predictions later on
#dem sen fs judge
scotus.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote))
scotus.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote))
scotus.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#dem sen nonfs judge
scotus.dem.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
scotus.dem.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
scotus.dem.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))

#rep sen fs judge
scotus.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote))
scotus.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote))
scotus.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#rep sen nonfs judge
scotus.rep.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
scotus.rep.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
scotus.rep.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))


#difference (dem fs- dem nonfs)
scotus.diff.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote))
scotus.diff.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote))
scotus.diff.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (rep fs-rep nonfs)
scotus.diff.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote))
scotus.diff.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote))
scotus.diff.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote))




#start the loops for the prediction(s)
for(i in 1:length(seq.vote)){
  #dem sen fs judge 
  scotus.pred.dem.fs=cbind(1,
                           data.scotus.reppres$Female,
                           data.scotus.reppres$ABANum,
                           data.scotus.reppres$EliteSchool,
                           0, #democrat senator
                           1, #fed soc jduge
                           data.scotus.reppres$SenatorDWNominate,
                           data.scotus.reppres$SenHomeStateSen,
                           data.scotus.reppres$JCS,
                           0, #interaction term
                           data.scotus.reppres[,12:315], #slope for Senator Unique ID
                           data.scotus.reppres[,316:327], #slope for Congress
                           data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  scotus.pred.dem.fs=as.matrix(scotus.pred.dem.fs)
  
  #dem sen nonfs judge 
  scotus.pred.dem.nonfs=cbind(1,
                              data.scotus.reppres$Female,
                              data.scotus.reppres$ABANum,
                              data.scotus.reppres$EliteSchool,
                              0, #democrat senator
                              0, #fed soc jduge
                              data.scotus.reppres$SenatorDWNominate,
                              data.scotus.reppres$SenHomeStateSen,
                              data.scotus.reppres$JCS,
                              0, #interaction term
                              data.scotus.reppres[,12:315], #slope for Senator Unique ID
                              data.scotus.reppres[,316:327], #slope for Congress
                              data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  scotus.pred.dem.nonfs=as.matrix(scotus.pred.dem.nonfs)
  
  #rep sen fs judge 
  scotus.pred.rep.fs=cbind(1,
                           data.scotus.reppres$Female,
                           data.scotus.reppres$ABANum,
                           data.scotus.reppres$EliteSchool,
                           1, #republican senator
                           1, #fedsoc judge
                           data.scotus.reppres$SenatorDWNominate,
                           data.scotus.reppres$SenHomeStateSen,
                           data.scotus.reppres$JCS,
                           1, #interaction term
                           data.scotus.reppres[,12:315], #slope for Senator Unique ID
                           data.scotus.reppres[,316:327], #slope for Congress
                           data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  scotus.pred.rep.fs=as.matrix(scotus.pred.rep.fs)
  
  #rep sen nonfs judge 
  scotus.pred.rep.nonfs=cbind(1,
                              data.scotus.reppres$Female,
                              data.scotus.reppres$ABANum,
                              data.scotus.reppres$EliteSchool,
                              1, #republican senator
                              0, #fedsoc judge
                              data.scotus.reppres$SenatorDWNominate,
                              data.scotus.reppres$SenHomeStateSen,
                              data.scotus.reppres$JCS,
                              0, #interaction term
                              data.scotus.reppres[,12:315], #slope for Senator Unique ID
                              data.scotus.reppres[,316:327], #slope for Congress
                              data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  scotus.pred.rep.nonfs=as.matrix(scotus.pred.rep.nonfs)
  
  
  #storage of predictions in next loop
  pp.scotus.dem.fs=matrix(NA,nrow=n.draws)
  pp.scotus.dem.nonfs=matrix(NA,nrow=n.draws)
  pp.scotus.rep.fs=matrix(NA,nrow=n.draws)
  pp.scotus.rep.nonfs=matrix(NA,nrow=n.draws)
  pp.scotus.diff.dem.fs=matrix(NA,nrow=n.draws)
  pp.scotus.diff.rep.fs=matrix(NA,nrow=n.draws)
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #dem 
    xb.scotus.dem.fs=plogis(scotus.pred.dem.fs%*%t(sim.base.int4[j,]))
    pp.scotus.dem.fs[j]=mean(xb.scotus.dem.fs)
    
    xb.scotus.dem.nonfs=plogis(scotus.pred.dem.nonfs%*%t(sim.base.int4[j,]))
    pp.scotus.dem.nonfs[j]=mean(xb.scotus.dem.nonfs)
    
    
    #rep
    xb.scotus.rep.fs=plogis(scotus.pred.rep.fs%*%t(sim.base.int4[j,]))
    pp.scotus.rep.fs[j]=mean(xb.scotus.rep.fs)
    
    xb.scotus.rep.nonfs=plogis(scotus.pred.rep.nonfs%*%t(sim.base.int4[j,]))
    pp.scotus.rep.nonfs[j]=mean(xb.scotus.rep.nonfs)
    
    #diff
    pp.scotus.diff.dem.fs[j]=mean(xb.scotus.dem.fs)-mean(xb.scotus.dem.nonfs)
    pp.scotus.diff.rep.fs[j]=mean(xb.scotus.rep.fs)-mean(xb.scotus.rep.nonfs)
  }
  #compute point estimates and CI for each
  scotus.dem.fs.mean[i]=mean(pp.scotus.dem.fs)
  scotus.dem.fs.hi[i]=quantile(pp.scotus.dem.fs,0.975)
  scotus.dem.fs.lo[i]=quantile(pp.scotus.dem.fs,0.025)
  
  scotus.dem.nonfs.mean[i]=mean(pp.scotus.dem.nonfs)
  scotus.dem.nonfs.hi[i]=quantile(pp.scotus.dem.nonfs,0.975)
  scotus.dem.nonfs.lo[i]=quantile(pp.scotus.dem.nonfs,0.025)
  
  scotus.rep.fs.mean[i]=mean(pp.scotus.rep.fs)
  scotus.rep.fs.hi[i]=quantile(pp.scotus.rep.fs,0.975)
  scotus.rep.fs.lo[i]=quantile(pp.scotus.rep.fs,0.025)
  
  scotus.rep.nonfs.mean[i]=mean(pp.scotus.rep.nonfs)
  scotus.rep.nonfs.hi[i]=quantile(pp.scotus.rep.nonfs,0.975)
  scotus.rep.nonfs.lo[i]=quantile(pp.scotus.rep.nonfs,0.025)
  
  scotus.diff.dem.fs.mean[i]=mean(pp.scotus.diff.dem.fs)
  scotus.diff.dem.fs.hi[i]=quantile(pp.scotus.diff.dem.fs,0.975)
  scotus.diff.dem.fs.lo[i]=quantile(pp.scotus.diff.dem.fs,0.025)
  
  scotus.diff.rep.fs.mean[i]=mean(pp.scotus.diff.rep.fs)
  scotus.diff.rep.fs.hi[i]=quantile(pp.scotus.diff.rep.fs,0.975)
  scotus.diff.rep.fs.lo[i]=quantile(pp.scotus.diff.rep.fs,0.025)
}




######################################################
######################## CoA #########################
######################################################


#set up matrices for predictions later on
#dem sen fs judge
coa.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote))
coa.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote))
coa.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#dem sen nonfs judge
coa.dem.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
coa.dem.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
coa.dem.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))

#rep sen fs judge
coa.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote))
coa.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote))
coa.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#rep sen nonfs judge
coa.rep.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
coa.rep.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
coa.rep.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))


#difference (dem fs- dem nonfs)
coa.diff.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote))
coa.diff.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote))
coa.diff.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (rep fs-rep nonfs)
coa.diff.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote))
coa.diff.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote))
coa.diff.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote))




#start the loops for the prediction(s)
for(i in 1:length(seq.vote)){
  #dem sen fs judge 
  coa.pred.dem.fs=cbind(1,
                        data.coa.reppres$Female,
                        data.coa.reppres$ABANum,
                        data.coa.reppres$EliteSchool,
                        0, #democrat senator
                        1, #fed soc jduge
                        data.coa.reppres$SenatorDWNominate,
                        data.coa.reppres$SenHomeStateSen,
                        data.coa.reppres$JCS,
                        0, #interaction term
                        data.coa.reppres[,12:315], #slope for Senator Unique ID
                        data.coa.reppres[,316:327], #slope for Congress
                        data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  coa.pred.dem.fs=as.matrix(coa.pred.dem.fs)
  
  #dem sen nonfs judge 
  coa.pred.dem.nonfs=cbind(1,
                           data.coa.reppres$Female,
                           data.coa.reppres$ABANum,
                           data.coa.reppres$EliteSchool,
                           0, #democrat senator
                           0, #fed soc jduge
                           data.coa.reppres$SenatorDWNominate,
                           data.coa.reppres$SenHomeStateSen,
                           data.coa.reppres$JCS,
                           0, #interaction term
                           data.coa.reppres[,12:315], #slope for Senator Unique ID
                           data.coa.reppres[,316:327], #slope for Congress
                           data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  coa.pred.dem.nonfs=as.matrix(coa.pred.dem.nonfs)
  
  #rep sen fs judge 
  coa.pred.rep.fs=cbind(1,
                        data.coa.reppres$Female,
                        data.coa.reppres$ABANum,
                        data.coa.reppres$EliteSchool,
                        1, #republican senator
                        1, #fedsoc judge
                        data.coa.reppres$SenatorDWNominate,
                        data.coa.reppres$SenHomeStateSen,
                        data.coa.reppres$JCS,
                        1, #interaction term
                        data.coa.reppres[,12:315], #slope for Senator Unique ID
                        data.coa.reppres[,316:327], #slope for Congress
                        data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  coa.pred.rep.fs=as.matrix(coa.pred.rep.fs)
  
  #rep sen nonfs judge 
  coa.pred.rep.nonfs=cbind(1,
                           data.coa.reppres$Female,
                           data.coa.reppres$ABANum,
                           data.coa.reppres$EliteSchool,
                           1, #republican senator
                           0, #fedsoc judge
                           data.coa.reppres$SenatorDWNominate,
                           data.coa.reppres$SenHomeStateSen,
                           data.coa.reppres$JCS,
                           0, #interaction term
                           data.coa.reppres[,12:315], #slope for Senator Unique ID
                           data.coa.reppres[,316:327], #slope for Congress
                           data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  coa.pred.rep.nonfs=as.matrix(coa.pred.rep.nonfs)
  
  
  #storage of predictions in next loop
  pp.coa.dem.fs=matrix(NA,nrow=n.draws)
  pp.coa.dem.nonfs=matrix(NA,nrow=n.draws)
  pp.coa.rep.fs=matrix(NA,nrow=n.draws)
  pp.coa.rep.nonfs=matrix(NA,nrow=n.draws)
  pp.coa.diff.dem.fs=matrix(NA,nrow=n.draws)
  pp.coa.diff.rep.fs=matrix(NA,nrow=n.draws)
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #dem 
    xb.coa.dem.fs=plogis(coa.pred.dem.fs%*%t(sim.base.int4[j,]))
    pp.coa.dem.fs[j]=mean(xb.coa.dem.fs)
    
    xb.coa.dem.nonfs=plogis(coa.pred.dem.nonfs%*%t(sim.base.int4[j,]))
    pp.coa.dem.nonfs[j]=mean(xb.coa.dem.nonfs)
    
    
    #rep
    xb.coa.rep.fs=plogis(coa.pred.rep.fs%*%t(sim.base.int4[j,]))
    pp.coa.rep.fs[j]=mean(xb.coa.rep.fs)
    
    xb.coa.rep.nonfs=plogis(coa.pred.rep.nonfs%*%t(sim.base.int4[j,]))
    pp.coa.rep.nonfs[j]=mean(xb.coa.rep.nonfs)
    
    #diff
    pp.coa.diff.dem.fs[j]=mean(xb.coa.dem.fs)-mean(xb.coa.dem.nonfs)
    pp.coa.diff.rep.fs[j]=mean(xb.coa.rep.fs)-mean(xb.coa.rep.nonfs)
  }
  #compute point estimates and CI for each
  coa.dem.fs.mean[i]=mean(pp.coa.dem.fs)
  coa.dem.fs.hi[i]=quantile(pp.coa.dem.fs,0.975)
  coa.dem.fs.lo[i]=quantile(pp.coa.dem.fs,0.025)
  
  coa.dem.nonfs.mean[i]=mean(pp.coa.dem.nonfs)
  coa.dem.nonfs.hi[i]=quantile(pp.coa.dem.nonfs,0.975)
  coa.dem.nonfs.lo[i]=quantile(pp.coa.dem.nonfs,0.025)
  
  coa.rep.fs.mean[i]=mean(pp.coa.rep.fs)
  coa.rep.fs.hi[i]=quantile(pp.coa.rep.fs,0.975)
  coa.rep.fs.lo[i]=quantile(pp.coa.rep.fs,0.025)
  
  coa.rep.nonfs.mean[i]=mean(pp.coa.rep.nonfs)
  coa.rep.nonfs.hi[i]=quantile(pp.coa.rep.nonfs,0.975)
  coa.rep.nonfs.lo[i]=quantile(pp.coa.rep.nonfs,0.025)
  
  coa.diff.dem.fs.mean[i]=mean(pp.coa.diff.dem.fs)
  coa.diff.dem.fs.hi[i]=quantile(pp.coa.diff.dem.fs,0.975)
  coa.diff.dem.fs.lo[i]=quantile(pp.coa.diff.dem.fs,0.025)
  
  coa.diff.rep.fs.mean[i]=mean(pp.coa.diff.rep.fs)
  coa.diff.rep.fs.hi[i]=quantile(pp.coa.diff.rep.fs,0.975)
  coa.diff.rep.fs.lo[i]=quantile(pp.coa.diff.rep.fs,0.025)
}





######################################################
################### District #########################
######################################################


#set up matrices for predictions later on
#dem sen fs judge
district.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote))
district.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote))
district.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#dem sen nonfs judge
district.dem.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
district.dem.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
district.dem.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))

#rep sen fs judge
district.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote))
district.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote))
district.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#rep sen nonfs judge
district.rep.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
district.rep.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
district.rep.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))


#difference (dem fs- dem nonfs)
district.diff.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote))
district.diff.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote))
district.diff.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (rep fs-rep nonfs)
district.diff.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote))
district.diff.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote))
district.diff.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote))




#start the loops for the prediction(s)
for(i in 1:length(seq.vote)){
  #dem sen fs judge 
  district.pred.dem.fs=cbind(1,
                             data.district.reppres$Female,
                             data.district.reppres$ABANum,
                             data.district.reppres$EliteSchool,
                             0, #democrat senator
                             1, #fed soc jduge
                             data.district.reppres$SenatorDWNominate,
                             data.district.reppres$SenHomeStateSen,
                             data.district.reppres$JCS,
                             0, #interaction term
                             data.district.reppres[,12:315], #slope for Senator Unique ID
                             data.district.reppres[,316:327], #slope for Congress
                             data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  district.pred.dem.fs=as.matrix(district.pred.dem.fs)
  
  #dem sen nonfs judge 
  district.pred.dem.nonfs=cbind(1,
                                data.district.reppres$Female,
                                data.district.reppres$ABANum,
                                data.district.reppres$EliteSchool,
                                0, #democrat senator
                                0, #fed soc jduge
                                data.district.reppres$SenatorDWNominate,
                                data.district.reppres$SenHomeStateSen,
                                data.district.reppres$JCS,
                                0, #interaction term
                                data.district.reppres[,12:315], #slope for Senator Unique ID
                                data.district.reppres[,316:327], #slope for Congress
                                data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  district.pred.dem.nonfs=as.matrix(district.pred.dem.nonfs)
  
  #rep sen fs judge 
  district.pred.rep.fs=cbind(1,
                             data.district.reppres$Female,
                             data.district.reppres$ABANum,
                             data.district.reppres$EliteSchool,
                             1, #republican senator
                             1, #fedsoc judge
                             data.district.reppres$SenatorDWNominate,
                             data.district.reppres$SenHomeStateSen,
                             data.district.reppres$JCS,
                             1, #interaction term
                             data.district.reppres[,12:315], #slope for Senator Unique ID
                             data.district.reppres[,316:327], #slope for Congress
                             data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  district.pred.rep.fs=as.matrix(district.pred.rep.fs)
  
  #rep sen nonfs judge 
  district.pred.rep.nonfs=cbind(1,
                                data.district.reppres$Female,
                                data.district.reppres$ABANum,
                                data.district.reppres$EliteSchool,
                                1, #republican senator
                                0, #fedsoc judge
                                data.district.reppres$SenatorDWNominate,
                                data.district.reppres$SenHomeStateSen,
                                data.district.reppres$JCS,
                                0, #interaction term
                                data.district.reppres[,12:315], #slope for Senator Unique ID
                                data.district.reppres[,316:327], #slope for Congress
                                data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  district.pred.rep.nonfs=as.matrix(district.pred.rep.nonfs)
  
  
  #storage of predictions in next loop
  pp.district.dem.fs=matrix(NA,nrow=n.draws)
  pp.district.dem.nonfs=matrix(NA,nrow=n.draws)
  pp.district.rep.fs=matrix(NA,nrow=n.draws)
  pp.district.rep.nonfs=matrix(NA,nrow=n.draws)
  pp.district.diff.dem.fs=matrix(NA,nrow=n.draws)
  pp.district.diff.rep.fs=matrix(NA,nrow=n.draws)
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #dem 
    xb.district.dem.fs=plogis(district.pred.dem.fs%*%t(sim.base.int4[j,]))
    pp.district.dem.fs[j]=mean(xb.district.dem.fs)
    
    xb.district.dem.nonfs=plogis(district.pred.dem.nonfs%*%t(sim.base.int4[j,]))
    pp.district.dem.nonfs[j]=mean(xb.district.dem.nonfs)
    
    
    #rep
    xb.district.rep.fs=plogis(district.pred.rep.fs%*%t(sim.base.int4[j,]))
    pp.district.rep.fs[j]=mean(xb.district.rep.fs)
    
    xb.district.rep.nonfs=plogis(district.pred.rep.nonfs%*%t(sim.base.int4[j,]))
    pp.district.rep.nonfs[j]=mean(xb.district.rep.nonfs)
    
    #diff
    pp.district.diff.dem.fs[j]=mean(xb.district.dem.fs)-mean(xb.district.dem.nonfs)
    pp.district.diff.rep.fs[j]=mean(xb.district.rep.fs)-mean(xb.district.rep.nonfs)
  }
  #compute point estimates and CI for each
  district.dem.fs.mean[i]=mean(pp.district.dem.fs)
  district.dem.fs.hi[i]=quantile(pp.district.dem.fs,0.975)
  district.dem.fs.lo[i]=quantile(pp.district.dem.fs,0.025)
  
  district.dem.nonfs.mean[i]=mean(pp.district.dem.nonfs)
  district.dem.nonfs.hi[i]=quantile(pp.district.dem.nonfs,0.975)
  district.dem.nonfs.lo[i]=quantile(pp.district.dem.nonfs,0.025)
  
  district.rep.fs.mean[i]=mean(pp.district.rep.fs)
  district.rep.fs.hi[i]=quantile(pp.district.rep.fs,0.975)
  district.rep.fs.lo[i]=quantile(pp.district.rep.fs,0.025)
  
  district.rep.nonfs.mean[i]=mean(pp.district.rep.nonfs)
  district.rep.nonfs.hi[i]=quantile(pp.district.rep.nonfs,0.975)
  district.rep.nonfs.lo[i]=quantile(pp.district.rep.nonfs,0.025)
  
  district.diff.dem.fs.mean[i]=mean(pp.district.diff.dem.fs)
  district.diff.dem.fs.hi[i]=quantile(pp.district.diff.dem.fs,0.975)
  district.diff.dem.fs.lo[i]=quantile(pp.district.diff.dem.fs,0.025)
  
  district.diff.rep.fs.mean[i]=mean(pp.district.diff.rep.fs)
  district.diff.rep.fs.hi[i]=quantile(pp.district.diff.rep.fs,0.975)
  district.diff.rep.fs.lo[i]=quantile(pp.district.diff.rep.fs,0.025)
}











####################################################################################################################################
####################################################################################################################################
### Base Interactive Model DW Nominate Predictions  (Republican-Only Nominees)
dem_sens_reppres=subset(noms_reppres,noms_reppres$SenParty==0)
rep_sens_reppres=subset(noms_reppres,noms_reppres$SenParty==1)
seq.vote.dw.dems.reppres=seq(from=min(na.omit(dem_sens_reppres$SenatorDWNominate)),to=max(na.omit(dem_sens_reppres$SenatorDWNominate)),by=0.01) #just to allow variation
seq.vote.dw.reps.reppres=seq(from=min(na.omit(rep_sens_reppres$SenatorDWNominate)),to=max(na.omit(rep_sens_reppres$SenatorDWNominate)),by=0.01) #just to allow variation
n.draws=1000 
sim.base.int3=coef(sim(mlm.vote.interaction3,n.draws))
sim.base.int3=as.data.frame(sim.base.int3)
####################################################################################################################################
####################################################################################################################################


######################################################
################### SCOTUS ###########################
######################################################
reppres.dw.scotus.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.scotus.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.scotus.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))

#dem sen nonfs judge
reppres.dw.scotus.dem.nonfs.mean=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.scotus.dem.nonfs.hi=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.scotus.dem.nonfs.lo=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))

#rep sen fs judge
reppres.dw.scotus.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.scotus.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.scotus.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))

#rep sen nonfs judge
reppres.dw.scotus.rep.nonfs.mean=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.scotus.rep.nonfs.hi=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.scotus.rep.nonfs.lo=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))


#difference (dem fs- dem nonfs)
reppres.dw.scotus.diff.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.scotus.diff.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.scotus.diff.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw))

#difference (rep fs-rep nonfs)
reppres.dw.scotus.diff.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.scotus.diff.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.scotus.diff.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw))



#### new sequences (dem vs. rep for DW nom) on untitled 5 tab


#start the loops for the prediction(s)
#start the loops for the prediction(s)
for(i in 1:length(seq.vote.dw.dems.reppres)){
  #dem sen fs judge 
  dw.scotus.pred.dem.fs.reppres=cbind(1,
                                      data.scotus.reppres$Female,
                                      data.scotus.reppres$ABANum,
                                      data.scotus.reppres$EliteSchool,
                                      0, #democrat senator
                                      1, #fed soc jduge
                                      seq.vote.dw.dems.reppres[i],
                                      data.scotus.reppres$SenHomeStateSen,
                                      data.scotus.reppres$JCS,
                                      seq.vote.dw.dems.reppres[i], #interaction term
                                      data.scotus.reppres[,12:315], #slope for Senator Unique ID
                                      data.scotus.reppres[,316:327], #slope for Congress
                                      data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.scotus.pred.dem.fs.reppres=as.matrix(dw.scotus.pred.dem.fs.reppres)
  
  #dem sen nonfs judge 
  dw.scotus.pred.dem.nonfs.reppres=cbind(1,
                                         data.scotus.reppres$Female,
                                         data.scotus.reppres$ABANum,
                                         data.scotus.reppres$EliteSchool,
                                         0, #democrat senator
                                         0, #fed soc jduge
                                         seq.vote.dw.dems.reppres[i],
                                         data.scotus.reppres$SenHomeStateSen,
                                         data.scotus.reppres$JCS,
                                         0, #interaction term
                                         data.scotus.reppres[,12:315], #slope for Senator Unique ID
                                         data.scotus.reppres[,316:327], #slope for Congress
                                         data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.scotus.pred.dem.nonfs.reppres=as.matrix(dw.scotus.pred.dem.nonfs.reppres)
  
  
  #storage of predictions in next loop
  dw.pp.scotus.dem.fs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.scotus.dem.nonfs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.scotus.diff.dem.fs.reppres=matrix(NA,nrow=n.draws)
  
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #dem 
    dw.xb.scotus.dem.fs.reppres=plogis(dw.scotus.pred.dem.fs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.scotus.dem.fs.reppres[j]=mean(dw.xb.scotus.dem.fs.reppres)
    
    dw.xb.scotus.dem.nonfs.reppres=plogis(dw.scotus.pred.dem.nonfs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.scotus.dem.nonfs.reppres[j]=mean(dw.xb.scotus.dem.nonfs.reppres)
    #diff
    dw.pp.scotus.diff.dem.fs.reppres[j]=mean(dw.xb.scotus.dem.fs.reppres)-mean(dw.xb.scotus.dem.nonfs.reppres)
  }
  #compute point estimates and CI for each
  reppres.dw.scotus.dem.fs.mean[i]=mean(dw.pp.scotus.dem.fs.reppres)
  reppres.dw.scotus.dem.fs.hi[i]=quantile(dw.pp.scotus.dem.fs.reppres,0.975)
  reppres.dw.scotus.dem.fs.lo[i]=quantile(dw.pp.scotus.dem.fs.reppres,0.025)
  
  reppres.dw.scotus.dem.nonfs.mean[i]=mean(dw.pp.scotus.dem.nonfs.reppres)
  reppres.dw.scotus.dem.nonfs.hi[i]=quantile(dw.pp.scotus.dem.nonfs.reppres,0.975)
  reppres.dw.scotus.dem.nonfs.lo[i]=quantile(dw.pp.scotus.dem.nonfs.reppres,0.025)
  
  reppres.dw.scotus.diff.dem.fs.mean[i]=mean(dw.pp.scotus.diff.dem.fs.reppres)
  reppres.dw.scotus.diff.dem.fs.hi[i]=quantile(dw.pp.scotus.diff.dem.fs.reppres,0.975)
  reppres.dw.scotus.diff.dem.fs.lo[i]=quantile(dw.pp.scotus.diff.dem.fs.reppres,0.025)
}




for(i in 1:length(seq.vote.dw.reps.reppres)){
  #rep sen fs judge 
  dw.scotus.pred.rep.fs.reppres=cbind(1,
                                      data.scotus.reppres$Female,
                                      data.scotus.reppres$ABANum,
                                      data.scotus.reppres$EliteSchool,
                                      1, #republican senator
                                      1, #fedsoc judge
                                      seq.vote.dw.reps.reppres[i],
                                      data.scotus.reppres$SenHomeStateSen,
                                      data.scotus.reppres$JCS,
                                      seq.vote.dw.reps.reppres[i], #interaction term
                                      data.scotus.reppres[,12:315], #slope for Senator Unique ID
                                      data.scotus.reppres[,316:327], #slope for Congress
                                      data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.scotus.pred.rep.fs.reppres=as.matrix(dw.scotus.pred.rep.fs.reppres)
  
  #rep sen nonfs judge 
  dw.scotus.pred.rep.nonfs.reppres=cbind(1,
                                         data.scotus.reppres$Female,
                                         data.scotus.reppres$ABANum,
                                         data.scotus.reppres$EliteSchool,
                                         1, #republican senator
                                         0, #fedsoc judge
                                         seq.vote.dw.reps.reppres[i],
                                         data.scotus.reppres$SenHomeStateSen,
                                         data.scotus.reppres$JCS,
                                         0, #interaction term
                                         data.scotus.reppres[,12:315], #slope for Senator Unique ID
                                         data.scotus.reppres[,316:327], #slope for Congress
                                         data.scotus.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.scotus.pred.rep.nonfs.reppres=as.matrix(dw.scotus.pred.rep.nonfs.reppres)
  
  
  dw.pp.scotus.rep.fs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.scotus.rep.nonfs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.scotus.diff.rep.fs.reppres=matrix(NA,nrow=n.draws)
  
  for(j in 1:n.draws){
    #rep
    dw.xb.scotus.rep.fs.reppres=plogis(dw.scotus.pred.rep.fs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.scotus.rep.fs.reppres[j]=mean(dw.xb.scotus.rep.fs.reppres)
    
    dw.xb.scotus.rep.nonfs.reppres=plogis(dw.scotus.pred.rep.nonfs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.scotus.rep.nonfs.reppres[j]=mean(dw.xb.scotus.rep.nonfs.reppres)
    
    #diff
    dw.pp.scotus.diff.rep.fs.reppres[j]=mean(dw.xb.scotus.rep.fs.reppres)-mean(dw.xb.scotus.rep.nonfs.reppres)
  }
  
  reppres.dw.scotus.rep.fs.mean[i]=mean(dw.pp.scotus.rep.fs.reppres)
  reppres.dw.scotus.rep.fs.hi[i]=quantile(dw.pp.scotus.rep.fs.reppres,0.975)
  reppres.dw.scotus.rep.fs.lo[i]=quantile(dw.pp.scotus.rep.fs.reppres,0.025)
  
  reppres.dw.scotus.rep.nonfs.mean[i]=mean(dw.pp.scotus.rep.nonfs.reppres)
  reppres.dw.scotus.rep.nonfs.hi[i]=quantile(dw.pp.scotus.rep.nonfs.reppres,0.975)
  reppres.dw.scotus.rep.nonfs.lo[i]=quantile(dw.pp.scotus.rep.nonfs.reppres,0.025)
  
  reppres.dw.scotus.diff.rep.fs.mean[i]=mean(dw.pp.scotus.diff.rep.fs.reppres)
  reppres.dw.scotus.diff.rep.fs.hi[i]=quantile(dw.pp.scotus.diff.rep.fs.reppres,0.975)
  reppres.dw.scotus.diff.rep.fs.lo[i]=quantile(dw.pp.scotus.diff.rep.fs.reppres,0.025)
}



######################################################
###################### CoA ###########################
######################################################
reppres.dw.coa.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.coa.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.coa.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))

#dem sen nonfs judge
reppres.dw.coa.dem.nonfs.mean=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.coa.dem.nonfs.hi=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.coa.dem.nonfs.lo=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))

#rep sen fs judge
reppres.dw.coa.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.coa.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.coa.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))

#rep sen nonfs judge
reppres.dw.coa.rep.nonfs.mean=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.coa.rep.nonfs.hi=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.coa.rep.nonfs.lo=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))


#difference (dem fs- dem nonfs)
reppres.dw.coa.diff.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.coa.diff.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.coa.diff.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw))

#difference (rep fs-rep nonfs)
reppres.dw.coa.diff.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.coa.diff.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.coa.diff.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw))



#### new sequences (dem vs. rep for DW nom) on untitled 5 tab


#start the loops for the prediction(s)
#start the loops for the prediction(s)
for(i in 1:length(seq.vote.dw.dems.reppres)){
  #dem sen fs judge 
  dw.coa.pred.dem.fs.reppres=cbind(1,
                                   data.coa.reppres$Female,
                                   data.coa.reppres$ABANum,
                                   data.coa.reppres$EliteSchool,
                                   0, #democrat senator
                                   1, #fed soc jduge
                                   seq.vote.dw.dems.reppres[i],
                                   data.coa.reppres$SenHomeStateSen,
                                   data.coa.reppres$JCS,
                                   seq.vote.dw.dems.reppres[i], #interaction term
                                   data.coa.reppres[,12:315], #slope for Senator Unique ID
                                   data.coa.reppres[,316:327], #slope for Congress
                                   data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.coa.pred.dem.fs.reppres=as.matrix(dw.coa.pred.dem.fs.reppres)
  
  #dem sen nonfs judge 
  dw.coa.pred.dem.nonfs.reppres=cbind(1,
                                      data.coa.reppres$Female,
                                      data.coa.reppres$ABANum,
                                      data.coa.reppres$EliteSchool,
                                      0, #democrat senator
                                      0, #fed soc jduge
                                      seq.vote.dw.dems.reppres[i],
                                      data.coa.reppres$SenHomeStateSen,
                                      data.coa.reppres$JCS,
                                      0, #interaction term
                                      data.coa.reppres[,12:315], #slope for Senator Unique ID
                                      data.coa.reppres[,316:327], #slope for Congress
                                      data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.coa.pred.dem.nonfs.reppres=as.matrix(dw.coa.pred.dem.nonfs.reppres)
  
  
  #storage of predictions in next loop
  dw.pp.coa.dem.fs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.coa.dem.nonfs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.coa.diff.dem.fs.reppres=matrix(NA,nrow=n.draws)
  
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #dem 
    dw.xb.coa.dem.fs.reppres=plogis(dw.coa.pred.dem.fs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.coa.dem.fs.reppres[j]=mean(dw.xb.coa.dem.fs.reppres)
    
    dw.xb.coa.dem.nonfs.reppres=plogis(dw.coa.pred.dem.nonfs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.coa.dem.nonfs.reppres[j]=mean(dw.xb.coa.dem.nonfs.reppres)
    #diff
    dw.pp.coa.diff.dem.fs.reppres[j]=mean(dw.xb.coa.dem.fs.reppres)-mean(dw.xb.coa.dem.nonfs.reppres)
  }
  #compute point estimates and CI for each
  reppres.dw.coa.dem.fs.mean[i]=mean(dw.pp.coa.dem.fs.reppres)
  reppres.dw.coa.dem.fs.hi[i]=quantile(dw.pp.coa.dem.fs.reppres,0.975)
  reppres.dw.coa.dem.fs.lo[i]=quantile(dw.pp.coa.dem.fs.reppres,0.025)
  
  reppres.dw.coa.dem.nonfs.mean[i]=mean(dw.pp.coa.dem.nonfs.reppres)
  reppres.dw.coa.dem.nonfs.hi[i]=quantile(dw.pp.coa.dem.nonfs.reppres,0.975)
  reppres.dw.coa.dem.nonfs.lo[i]=quantile(dw.pp.coa.dem.nonfs.reppres,0.025)
  
  reppres.dw.coa.diff.dem.fs.mean[i]=mean(dw.pp.coa.diff.dem.fs.reppres)
  reppres.dw.coa.diff.dem.fs.hi[i]=quantile(dw.pp.coa.diff.dem.fs.reppres,0.975)
  reppres.dw.coa.diff.dem.fs.lo[i]=quantile(dw.pp.coa.diff.dem.fs.reppres,0.025)
}




for(i in 1:length(seq.vote.dw.reps.reppres)){
  #rep sen fs judge 
  dw.coa.pred.rep.fs.reppres=cbind(1,
                                   data.coa.reppres$Female,
                                   data.coa.reppres$ABANum,
                                   data.coa.reppres$EliteSchool,
                                   1, #republican senator
                                   1, #fedsoc judge
                                   seq.vote.dw.reps.reppres[i],
                                   data.coa.reppres$SenHomeStateSen,
                                   data.coa.reppres$JCS,
                                   seq.vote.dw.reps.reppres[i], #interaction term
                                   data.coa.reppres[,12:315], #slope for Senator Unique ID
                                   data.coa.reppres[,316:327], #slope for Congress
                                   data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.coa.pred.rep.fs.reppres=as.matrix(dw.coa.pred.rep.fs.reppres)
  
  #rep sen nonfs judge 
  dw.coa.pred.rep.nonfs.reppres=cbind(1,
                                      data.coa.reppres$Female,
                                      data.coa.reppres$ABANum,
                                      data.coa.reppres$EliteSchool,
                                      1, #republican senator
                                      0, #fedsoc judge
                                      seq.vote.dw.reps.reppres[i],
                                      data.coa.reppres$SenHomeStateSen,
                                      data.coa.reppres$JCS,
                                      0, #interaction term
                                      data.coa.reppres[,12:315], #slope for Senator Unique ID
                                      data.coa.reppres[,316:327], #slope for Congress
                                      data.coa.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.coa.pred.rep.nonfs.reppres=as.matrix(dw.coa.pred.rep.nonfs.reppres)
  
  
  dw.pp.coa.rep.fs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.coa.rep.nonfs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.coa.diff.rep.fs.reppres=matrix(NA,nrow=n.draws)
  
  for(j in 1:n.draws){
    #rep
    dw.xb.coa.rep.fs.reppres=plogis(dw.coa.pred.rep.fs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.coa.rep.fs.reppres[j]=mean(dw.xb.coa.rep.fs.reppres)
    
    dw.xb.coa.rep.nonfs.reppres=plogis(dw.coa.pred.rep.nonfs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.coa.rep.nonfs.reppres[j]=mean(dw.xb.coa.rep.nonfs.reppres)
    
    #diff
    dw.pp.coa.diff.rep.fs.reppres[j]=mean(dw.xb.coa.rep.fs.reppres)-mean(dw.xb.coa.rep.nonfs.reppres)
  }
  
  reppres.dw.coa.rep.fs.mean[i]=mean(dw.pp.coa.rep.fs.reppres)
  reppres.dw.coa.rep.fs.hi[i]=quantile(dw.pp.coa.rep.fs.reppres,0.975)
  reppres.dw.coa.rep.fs.lo[i]=quantile(dw.pp.coa.rep.fs.reppres,0.025)
  
  reppres.dw.coa.rep.nonfs.mean[i]=mean(dw.pp.coa.rep.nonfs.reppres)
  reppres.dw.coa.rep.nonfs.hi[i]=quantile(dw.pp.coa.rep.nonfs.reppres,0.975)
  reppres.dw.coa.rep.nonfs.lo[i]=quantile(dw.pp.coa.rep.nonfs.reppres,0.025)
  
  reppres.dw.coa.diff.rep.fs.mean[i]=mean(dw.pp.coa.diff.rep.fs.reppres)
  reppres.dw.coa.diff.rep.fs.hi[i]=quantile(dw.pp.coa.diff.rep.fs.reppres,0.975)
  reppres.dw.coa.diff.rep.fs.lo[i]=quantile(dw.pp.coa.diff.rep.fs.reppres,0.025)
}



######################################################
#################### District ########################
######################################################
reppres.dw.district.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.district.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.district.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))

#dem sen nonfs judge
reppres.dw.district.dem.nonfs.mean=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.district.dem.nonfs.hi=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))
reppres.dw.district.dem.nonfs.lo=as.matrix(NA,nrow=length(seq.vote.dw.dems.reppres))

#rep sen fs judge
reppres.dw.district.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.district.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.district.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))

#rep sen nonfs judge
reppres.dw.district.rep.nonfs.mean=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.district.rep.nonfs.hi=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))
reppres.dw.district.rep.nonfs.lo=as.matrix(NA,nrow=length(seq.vote.dw.reps.reppres))


#difference (dem fs- dem nonfs)
reppres.dw.district.diff.dem.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.district.diff.dem.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.district.diff.dem.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw))

#difference (rep fs-rep nonfs)
reppres.dw.district.diff.rep.fs.mean=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.district.diff.rep.fs.hi=as.matrix(NA,nrow=length(seq.vote.dw))
reppres.dw.district.diff.rep.fs.lo=as.matrix(NA,nrow=length(seq.vote.dw))



#### new sequences (dem vs. rep for DW nom) on untitled 5 tab


#start the loops for the prediction(s)
#start the loops for the prediction(s)
for(i in 1:length(seq.vote.dw.dems.reppres)){
  #dem sen fs judge 
  dw.district.pred.dem.fs.reppres=cbind(1,
                                        data.district.reppres$Female,
                                        data.district.reppres$ABANum,
                                        data.district.reppres$EliteSchool,
                                        0, #democrat senator
                                        1, #fed soc jduge
                                        seq.vote.dw.dems.reppres[i],
                                        data.district.reppres$SenHomeStateSen,
                                        data.district.reppres$JCS,
                                        seq.vote.dw.dems.reppres[i], #interaction term
                                        data.district.reppres[,12:315], #slope for Senator Unique ID
                                        data.district.reppres[,316:327], #slope for Congress
                                        data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.district.pred.dem.fs.reppres=as.matrix(dw.district.pred.dem.fs.reppres)
  
  #dem sen nonfs judge 
  dw.district.pred.dem.nonfs.reppres=cbind(1,
                                           data.district.reppres$Female,
                                           data.district.reppres$ABANum,
                                           data.district.reppres$EliteSchool,
                                           0, #democrat senator
                                           0, #fed soc jduge
                                           seq.vote.dw.dems.reppres[i],
                                           data.district.reppres$SenHomeStateSen,
                                           data.district.reppres$JCS,
                                           0, #interaction term
                                           data.district.reppres[,12:315], #slope for Senator Unique ID
                                           data.district.reppres[,316:327], #slope for Congress
                                           data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.district.pred.dem.nonfs.reppres=as.matrix(dw.district.pred.dem.nonfs.reppres)
  
  
  #storage of predictions in next loop
  dw.pp.district.dem.fs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.district.dem.nonfs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.district.diff.dem.fs.reppres=matrix(NA,nrow=n.draws)
  
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #dem 
    dw.xb.district.dem.fs.reppres=plogis(dw.district.pred.dem.fs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.district.dem.fs.reppres[j]=mean(dw.xb.district.dem.fs.reppres)
    
    dw.xb.district.dem.nonfs.reppres=plogis(dw.district.pred.dem.nonfs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.district.dem.nonfs.reppres[j]=mean(dw.xb.district.dem.nonfs.reppres)
    #diff
    dw.pp.district.diff.dem.fs.reppres[j]=mean(dw.xb.district.dem.fs.reppres)-mean(dw.xb.district.dem.nonfs.reppres)
  }
  #compute point estimates and CI for each
  reppres.dw.district.dem.fs.mean[i]=mean(dw.pp.district.dem.fs.reppres)
  reppres.dw.district.dem.fs.hi[i]=quantile(dw.pp.district.dem.fs.reppres,0.975)
  reppres.dw.district.dem.fs.lo[i]=quantile(dw.pp.district.dem.fs.reppres,0.025)
  
  reppres.dw.district.dem.nonfs.mean[i]=mean(dw.pp.district.dem.nonfs.reppres)
  reppres.dw.district.dem.nonfs.hi[i]=quantile(dw.pp.district.dem.nonfs.reppres,0.975)
  reppres.dw.district.dem.nonfs.lo[i]=quantile(dw.pp.district.dem.nonfs.reppres,0.025)
  
  reppres.dw.district.diff.dem.fs.mean[i]=mean(dw.pp.district.diff.dem.fs.reppres)
  reppres.dw.district.diff.dem.fs.hi[i]=quantile(dw.pp.district.diff.dem.fs.reppres,0.975)
  reppres.dw.district.diff.dem.fs.lo[i]=quantile(dw.pp.district.diff.dem.fs.reppres,0.025)
}




for(i in 1:length(seq.vote.dw.reps.reppres)){
  #rep sen fs judge 
  dw.district.pred.rep.fs.reppres=cbind(1,
                                        data.district.reppres$Female,
                                        data.district.reppres$ABANum,
                                        data.district.reppres$EliteSchool,
                                        1, #republican senator
                                        1, #fedsoc judge
                                        seq.vote.dw.reps.reppres[i],
                                        data.district.reppres$SenHomeStateSen,
                                        data.district.reppres$JCS,
                                        seq.vote.dw.reps.reppres[i], #interaction term
                                        data.district.reppres[,12:315], #slope for Senator Unique ID
                                        data.district.reppres[,316:327], #slope for Congress
                                        data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.district.pred.rep.fs.reppres=as.matrix(dw.district.pred.rep.fs.reppres)
  
  #rep sen nonfs judge 
  dw.district.pred.rep.nonfs.reppres=cbind(1,
                                           data.district.reppres$Female,
                                           data.district.reppres$ABANum,
                                           data.district.reppres$EliteSchool,
                                           1, #republican senator
                                           0, #fedsoc judge
                                           seq.vote.dw.reps.reppres[i],
                                           data.district.reppres$SenHomeStateSen,
                                           data.district.reppres$JCS,
                                           0, #interaction term
                                           data.district.reppres[,12:315], #slope for Senator Unique ID
                                           data.district.reppres[,316:327], #slope for Congress
                                           data.district.reppres[,328:332]) #slope for court level indicator (dichotomous here but need them for comformabe)
  dw.district.pred.rep.nonfs.reppres=as.matrix(dw.district.pred.rep.nonfs.reppres)
  
  
  dw.pp.district.rep.fs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.district.rep.nonfs.reppres=matrix(NA,nrow=n.draws)
  dw.pp.district.diff.rep.fs.reppres=matrix(NA,nrow=n.draws)
  
  for(j in 1:n.draws){
    #rep
    dw.xb.district.rep.fs.reppres=plogis(dw.district.pred.rep.fs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.district.rep.fs.reppres[j]=mean(dw.xb.district.rep.fs.reppres)
    
    dw.xb.district.rep.nonfs.reppres=plogis(dw.district.pred.rep.nonfs.reppres%*%t(sim.base.int3[j,]))
    dw.pp.district.rep.nonfs.reppres[j]=mean(dw.xb.district.rep.nonfs.reppres)
    
    #diff
    dw.pp.district.diff.rep.fs.reppres[j]=mean(dw.xb.district.rep.fs.reppres)-mean(dw.xb.district.rep.nonfs.reppres)
  }
  
  reppres.dw.district.rep.fs.mean[i]=mean(dw.pp.district.rep.fs.reppres)
  reppres.dw.district.rep.fs.hi[i]=quantile(dw.pp.district.rep.fs.reppres,0.975)
  reppres.dw.district.rep.fs.lo[i]=quantile(dw.pp.district.rep.fs.reppres,0.025)
  
  reppres.dw.district.rep.nonfs.mean[i]=mean(dw.pp.district.rep.nonfs.reppres)
  reppres.dw.district.rep.nonfs.hi[i]=quantile(dw.pp.district.rep.nonfs.reppres,0.975)
  reppres.dw.district.rep.nonfs.lo[i]=quantile(dw.pp.district.rep.nonfs.reppres,0.025)
  
  reppres.dw.district.diff.rep.fs.mean[i]=mean(dw.pp.district.diff.rep.fs.reppres)
  reppres.dw.district.diff.rep.fs.hi[i]=quantile(dw.pp.district.diff.rep.fs.reppres,0.975)
  reppres.dw.district.diff.rep.fs.lo[i]=quantile(dw.pp.district.diff.rep.fs.reppres,0.025)
}









####################################################################################################################################
####################################################################################################################################
### Republican Only: FedSoc Senators*FedSocNoms
n.draws=1000 
sim.rep.reppres.fsint=coef(sim(mlm.vote.republican.reppres.int,n.draws))
sim.rep.reppres.fsint=as.data.frame(sim.rep.reppres.fsint)
seq.vote=seq(from=0,to=1) #just to allow variation
####################################################################################################################################
####################################################################################################################################


######################################################
#################### SCOTUS  #########################
######################################################
#set up matrices for predictions later on
#sen fs, fs judge
reppres.scotus.senfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.senfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.senfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen fs, nonfs judge
reppres.scotus.senfs.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.senfs.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.senfs.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen nonfs, fs judge
reppres.scotus.sennonfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.sennonfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.sennonfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen nonfs, nonfs judge
reppres.scotus.sennonfs.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.sennonfs.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.sennonfs.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))


#difference (sen fs; fs-nonfs)
reppres.scotus.diff.senfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.senfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.senfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen nonfs; fs-nonfs)
reppres.scotus.diff.sennonfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.sennonfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.sennonfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen fs - non fs for fs noms)
reppres.scotus.diff.senfsnon.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.senfsnon.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.senfsnon.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen fs - non fs for nonfs noms)
reppres.scotus.diff.senfsnon.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.senfsnon.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.scotus.diff.senfsnon.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))



for(i in 1:length(seq.vote)){
  #sen fs, fs judge 
  reppres.scotus.pred.senfs.fs=cbind(1,
                                     data.scotus_rep$Female,
                                     data.scotus_rep$ABANum,
                                     data.scotus_rep$EliteSchool,
                                     1, #fed soc judge,
                                     data.scotus_rep$SenatorDWNominate,
                                     data.scotus_rep$SenHomeStateSen,
                                     1, #fed soc senator
                                     data.scotus_rep$JCS,
                                     1, #interaction judge/senator
                                     data.scotus_rep[,9:180], #slope for Senator Unique ID
                                     data.scotus_rep[,181:199], #slope for Congress
                                     data.scotus_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.scotus.pred.senfs.fs=as.matrix(reppres.scotus.pred.senfs.fs)
  
  #sen fs, nonfs judge
  reppres.scotus.pred.senfs.nonfs=cbind(1,
                                        data.scotus_rep$Female,
                                        data.scotus_rep$ABANum,
                                        data.scotus_rep$EliteSchool,
                                        0, #fed soc judge
                                        data.scotus_rep$SenatorDWNominate,
                                        data.scotus_rep$SenHomeStateSen,
                                        1, #fed soc senator
                                        data.scotus_rep$JCS,
                                        0, #interaction judge/senator
                                        data.scotus_rep[,9:180], #slope for Senator Unique ID
                                        data.scotus_rep[,181:199], #slope for Congress
                                        data.scotus_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.scotus.pred.senfs.nonfs=as.matrix(reppres.scotus.pred.senfs.nonfs)
  
  #sen nonfs, fs judge
  reppres.scotus.pred.sennonfs.fs=cbind(1,
                                        data.scotus_rep$Female,
                                        data.scotus_rep$ABANum,
                                        data.scotus_rep$EliteSchool,
                                        1, #fed soc judge
                                        data.scotus_rep$SenatorDWNominate,
                                        data.scotus_rep$SenHomeStateSen,
                                        0, #fed soc senator
                                        data.scotus_rep$JCS,
                                        0, #interaction judge/senator
                                        data.scotus_rep[,9:180], #slope for Senator Unique ID
                                        data.scotus_rep[,181:199], #slope for Congress
                                        data.scotus_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.scotus.pred.sennonfs.fs=as.matrix(reppres.scotus.pred.sennonfs.fs)
  
  #sen nonfs, nonfs judge
  reppres.scotus.pred.sennonfs.nonfs=cbind(1,
                                           data.scotus_rep$Female,
                                           data.scotus_rep$ABANum,
                                           data.scotus_rep$EliteSchool,
                                           0, #fed soc judge
                                           data.scotus_rep$SenatorDWNominate,
                                           data.scotus_rep$SenHomeStateSen,
                                           0, #fed soc senator
                                           data.scotus_rep$JCS,
                                           0, #interaction judge/senator
                                           data.scotus_rep[,9:180], #slope for Senator Unique ID
                                           data.scotus_rep[,181:199], #slope for Congress
                                           data.scotus_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.scotus.pred.sennonfs.nonfs=as.matrix(reppres.scotus.pred.sennonfs.nonfs)
  
  
  
  
  #storage of predictions in next loop
  pp.reppres.scotus.senfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.senfs.nonfs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.sennonfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.sennonfs.nonfs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.diff.senfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.diff.sennonfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.diff.senfsnon.fs=matrix(NA,nrow=n.draws)
  pp.reppres.scotus.diff.senfsnon.nonfs=matrix(NA,nrow=n.draws)
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #sen fs 
    xb.reppres.scotus.senfs.fs=plogis(reppres.scotus.pred.senfs.fs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.scotus.senfs.fs[j]=mean(xb.reppres.scotus.senfs.fs)
    
    xb.reppres.scotus.senfs.nonfs=plogis(reppres.scotus.pred.senfs.nonfs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.scotus.senfs.nonfs[j]=mean(xb.reppres.scotus.senfs.nonfs)
    
    
    #sen non fs
    xb.reppres.scotus.sennonfs.fs=plogis(reppres.scotus.pred.sennonfs.fs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.scotus.sennonfs.fs[j]=mean(xb.reppres.scotus.sennonfs.fs)
    
    xb.reppres.scotus.sennonfs.nonfs=plogis(reppres.scotus.pred.sennonfs.nonfs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.scotus.sennonfs.nonfs[j]=mean(xb.reppres.scotus.sennonfs.nonfs)
    
    #diff
    pp.reppres.scotus.diff.senfs.fs[j]=mean(xb.reppres.scotus.senfs.fs)-mean(xb.reppres.scotus.senfs.nonfs)
    pp.reppres.scotus.diff.sennonfs.fs[j]=mean(xb.reppres.scotus.sennonfs.fs)-mean(xb.reppres.scotus.sennonfs.nonfs)
    
    pp.reppres.scotus.diff.senfsnon.fs[j]=mean(xb.reppres.scotus.senfs.fs)-mean(xb.reppres.scotus.sennonfs.fs)
    pp.reppres.scotus.diff.senfsnon.nonfs[j]=mean(xb.reppres.scotus.senfs.nonfs)-mean(xb.reppres.scotus.sennonfs.nonfs)
  }
  #compute point estimates and CI for each
  reppres.scotus.senfs.fs.mean[i]=mean(pp.reppres.scotus.senfs.fs)
  reppres.scotus.senfs.fs.hi[i]=quantile(pp.reppres.scotus.senfs.fs,0.975)
  reppres.scotus.senfs.fs.lo[i]=quantile(pp.reppres.scotus.senfs.fs,0.025)
  
  reppres.scotus.senfs.nonfs.mean[i]=mean(pp.reppres.scotus.senfs.nonfs)
  reppres.scotus.senfs.nonfs.hi[i]=quantile(pp.reppres.scotus.senfs.nonfs,0.975)
  reppres.scotus.senfs.nonfs.lo[i]=quantile(pp.reppres.scotus.senfs.nonfs,0.025)
  
  reppres.scotus.sennonfs.fs.mean[i]=mean(pp.reppres.scotus.sennonfs.fs)
  reppres.scotus.sennonfs.fs.hi[i]=quantile(pp.reppres.scotus.sennonfs.fs,0.975)
  reppres.scotus.sennonfs.fs.lo[i]=quantile(pp.reppres.scotus.sennonfs.fs,0.025)
  
  reppres.scotus.sennonfs.nonfs.mean[i]=mean(pp.reppres.scotus.sennonfs.nonfs)
  reppres.scotus.sennonfs.nonfs.hi[i]=quantile(pp.reppres.scotus.sennonfs.nonfs,0.975)
  reppres.scotus.sennonfs.nonfs.lo[i]=quantile(pp.reppres.scotus.sennonfs.nonfs,0.025)
  
  reppres.scotus.diff.senfs.fs.mean[i]=mean(pp.reppres.scotus.diff.senfs.fs)
  reppres.scotus.diff.senfs.fs.hi[i]=quantile(pp.reppres.scotus.diff.senfs.fs,0.975)
  reppres.scotus.diff.senfs.fs.lo[i]=quantile(pp.reppres.scotus.diff.senfs.fs,0.025)
  
  reppres.scotus.diff.sennonfs.fs.mean[i]=mean(pp.reppres.scotus.diff.sennonfs.fs)
  reppres.scotus.diff.sennonfs.fs.hi[i]=quantile(pp.reppres.scotus.diff.sennonfs.fs,0.975)
  reppres.scotus.diff.sennonfs.fs.lo[i]=quantile(pp.reppres.scotus.diff.sennonfs.fs,0.025)
  
  reppres.scotus.diff.senfsnon.fs.mean[i]=mean(pp.reppres.scotus.diff.senfsnon.fs)
  reppres.scotus.diff.senfsnon.fs.hi[i]=quantile(pp.reppres.scotus.diff.senfsnon.fs,0.975)
  reppres.scotus.diff.senfsnon.fs.lo[i]=quantile(pp.reppres.scotus.diff.senfsnon.fs,0.025)
  
  reppres.scotus.diff.senfsnon.nonfs.mean[i]=mean(pp.reppres.scotus.diff.senfsnon.nonfs)
  reppres.scotus.diff.senfsnon.nonfs.hi[i]=quantile(pp.reppres.scotus.diff.senfsnon.nonfs,0.975)
  reppres.scotus.diff.senfsnon.nonfs.lo[i]=quantile(pp.reppres.scotus.diff.senfsnon.nonfs,0.025)
}





######################################################
####################   CoA   #########################
######################################################
#set up matrices for predictions later on
#sen fs, fs judge
reppres.coa.senfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.senfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.senfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen fs, nonfs judge
reppres.coa.senfs.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.senfs.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.senfs.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen nonfs, fs judge
reppres.coa.sennonfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.sennonfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.sennonfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen nonfs, nonfs judge
reppres.coa.sennonfs.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.sennonfs.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.sennonfs.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))


#difference (sen fs; fs-nonfs)
reppres.coa.diff.senfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.senfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.senfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen nonfs; fs-nonfs)
reppres.coa.diff.sennonfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.sennonfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.sennonfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen fs - non fs for fs noms)
reppres.coa.diff.senfsnon.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.senfsnon.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.senfsnon.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen fs - non fs for nonfs noms)
reppres.coa.diff.senfsnon.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.senfsnon.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.coa.diff.senfsnon.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))



for(i in 1:length(seq.vote)){
  #sen fs, fs judge 
  reppres.coa.pred.senfs.fs=cbind(1,
                                  data.coa_rep$Female,
                                  data.coa_rep$ABANum,
                                  data.coa_rep$EliteSchool,
                                  1, #fed soc judge,
                                  data.coa_rep$SenatorDWNominate,
                                  data.coa_rep$SenHomeStateSen,
                                  1, #fed soc senator
                                  data.coa_rep$JCS,
                                  1, #interaction judge/senator
                                  data.coa_rep[,9:180], #slope for Senator Unique ID
                                  data.coa_rep[,181:199], #slope for Congress
                                  data.coa_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.coa.pred.senfs.fs=as.matrix(reppres.coa.pred.senfs.fs)
  
  #sen fs, nonfs judge
  reppres.coa.pred.senfs.nonfs=cbind(1,
                                     data.coa_rep$Female,
                                     data.coa_rep$ABANum,
                                     data.coa_rep$EliteSchool,
                                     0, #fed soc judge
                                     data.coa_rep$SenatorDWNominate,
                                     data.coa_rep$SenHomeStateSen,
                                     1, #fed soc senator
                                     data.coa_rep$JCS,
                                     0, #interaction judge/senator
                                     data.coa_rep[,9:180], #slope for Senator Unique ID
                                     data.coa_rep[,181:199], #slope for Congress
                                     data.coa_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.coa.pred.senfs.nonfs=as.matrix(reppres.coa.pred.senfs.nonfs)
  
  #sen nonfs, fs judge
  reppres.coa.pred.sennonfs.fs=cbind(1,
                                     data.coa_rep$Female,
                                     data.coa_rep$ABANum,
                                     data.coa_rep$EliteSchool,
                                     1, #fed soc judge
                                     data.coa_rep$SenatorDWNominate,
                                     data.coa_rep$SenHomeStateSen,
                                     0, #fed soc senator
                                     data.coa_rep$JCS,
                                     0, #interaction judge/senator
                                     data.coa_rep[,9:180], #slope for Senator Unique ID
                                     data.coa_rep[,181:199], #slope for Congress
                                     data.coa_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.coa.pred.sennonfs.fs=as.matrix(reppres.coa.pred.sennonfs.fs)
  
  #sen nonfs, nonfs judge
  reppres.coa.pred.sennonfs.nonfs=cbind(1,
                                        data.coa_rep$Female,
                                        data.coa_rep$ABANum,
                                        data.coa_rep$EliteSchool,
                                        0, #fed soc judge
                                        data.coa_rep$SenatorDWNominate,
                                        data.coa_rep$SenHomeStateSen,
                                        0, #fed soc senator
                                        data.coa_rep$JCS,
                                        0, #interaction judge/senator
                                        data.coa_rep[,9:180], #slope for Senator Unique ID
                                        data.coa_rep[,181:199], #slope for Congress
                                        data.coa_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.coa.pred.sennonfs.nonfs=as.matrix(reppres.coa.pred.sennonfs.nonfs)
  
  
  
  
  #storage of predictions in next loop
  pp.reppres.coa.senfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.senfs.nonfs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.sennonfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.sennonfs.nonfs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.diff.senfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.diff.sennonfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.diff.senfsnon.fs=matrix(NA,nrow=n.draws)
  pp.reppres.coa.diff.senfsnon.nonfs=matrix(NA,nrow=n.draws)
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #sen fs 
    xb.reppres.coa.senfs.fs=plogis(reppres.coa.pred.senfs.fs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.coa.senfs.fs[j]=mean(xb.reppres.coa.senfs.fs)
    
    xb.reppres.coa.senfs.nonfs=plogis(reppres.coa.pred.senfs.nonfs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.coa.senfs.nonfs[j]=mean(xb.reppres.coa.senfs.nonfs)
    
    
    #sen non fs
    xb.reppres.coa.sennonfs.fs=plogis(reppres.coa.pred.sennonfs.fs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.coa.sennonfs.fs[j]=mean(xb.reppres.coa.sennonfs.fs)
    
    xb.reppres.coa.sennonfs.nonfs=plogis(reppres.coa.pred.sennonfs.nonfs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.coa.sennonfs.nonfs[j]=mean(xb.reppres.coa.sennonfs.nonfs)
    
    #diff
    pp.reppres.coa.diff.senfs.fs[j]=mean(xb.reppres.coa.senfs.fs)-mean(xb.reppres.coa.senfs.nonfs)
    pp.reppres.coa.diff.sennonfs.fs[j]=mean(xb.reppres.coa.sennonfs.fs)-mean(xb.reppres.coa.sennonfs.nonfs)
    
    pp.reppres.coa.diff.senfsnon.fs[j]=mean(xb.reppres.coa.senfs.fs)-mean(xb.reppres.coa.sennonfs.fs)
    pp.reppres.coa.diff.senfsnon.nonfs[j]=mean(xb.reppres.coa.senfs.nonfs)-mean(xb.reppres.coa.sennonfs.nonfs)
  }
  #compute point estimates and CI for each
  reppres.coa.senfs.fs.mean[i]=mean(pp.reppres.coa.senfs.fs)
  reppres.coa.senfs.fs.hi[i]=quantile(pp.reppres.coa.senfs.fs,0.975)
  reppres.coa.senfs.fs.lo[i]=quantile(pp.reppres.coa.senfs.fs,0.025)
  
  reppres.coa.senfs.nonfs.mean[i]=mean(pp.reppres.coa.senfs.nonfs)
  reppres.coa.senfs.nonfs.hi[i]=quantile(pp.reppres.coa.senfs.nonfs,0.975)
  reppres.coa.senfs.nonfs.lo[i]=quantile(pp.reppres.coa.senfs.nonfs,0.025)
  
  reppres.coa.sennonfs.fs.mean[i]=mean(pp.reppres.coa.sennonfs.fs)
  reppres.coa.sennonfs.fs.hi[i]=quantile(pp.reppres.coa.sennonfs.fs,0.975)
  reppres.coa.sennonfs.fs.lo[i]=quantile(pp.reppres.coa.sennonfs.fs,0.025)
  
  reppres.coa.sennonfs.nonfs.mean[i]=mean(pp.reppres.coa.sennonfs.nonfs)
  reppres.coa.sennonfs.nonfs.hi[i]=quantile(pp.reppres.coa.sennonfs.nonfs,0.975)
  reppres.coa.sennonfs.nonfs.lo[i]=quantile(pp.reppres.coa.sennonfs.nonfs,0.025)
  
  reppres.coa.diff.senfs.fs.mean[i]=mean(pp.reppres.coa.diff.senfs.fs)
  reppres.coa.diff.senfs.fs.hi[i]=quantile(pp.reppres.coa.diff.senfs.fs,0.975)
  reppres.coa.diff.senfs.fs.lo[i]=quantile(pp.reppres.coa.diff.senfs.fs,0.025)
  
  reppres.coa.diff.sennonfs.fs.mean[i]=mean(pp.reppres.coa.diff.sennonfs.fs)
  reppres.coa.diff.sennonfs.fs.hi[i]=quantile(pp.reppres.coa.diff.sennonfs.fs,0.975)
  reppres.coa.diff.sennonfs.fs.lo[i]=quantile(pp.reppres.coa.diff.sennonfs.fs,0.025)
  
  reppres.coa.diff.senfsnon.fs.mean[i]=mean(pp.reppres.coa.diff.senfsnon.fs)
  reppres.coa.diff.senfsnon.fs.hi[i]=quantile(pp.reppres.coa.diff.senfsnon.fs,0.975)
  reppres.coa.diff.senfsnon.fs.lo[i]=quantile(pp.reppres.coa.diff.senfsnon.fs,0.025)
  
  reppres.coa.diff.senfsnon.nonfs.mean[i]=mean(pp.reppres.coa.diff.senfsnon.nonfs)
  reppres.coa.diff.senfsnon.nonfs.hi[i]=quantile(pp.reppres.coa.diff.senfsnon.nonfs,0.975)
  reppres.coa.diff.senfsnon.nonfs.lo[i]=quantile(pp.reppres.coa.diff.senfsnon.nonfs,0.025)
}


######################################################
#################### District  #######################
######################################################
#set up matrices for predictions later on
#sen fs, fs judge
reppres.district.senfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.senfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.senfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen fs, nonfs judge
reppres.district.senfs.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.senfs.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.senfs.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen nonfs, fs judge
reppres.district.sennonfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.sennonfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.sennonfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#sen nonfs, nonfs judge
reppres.district.sennonfs.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.sennonfs.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.sennonfs.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))


#difference (sen fs; fs-nonfs)
reppres.district.diff.senfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.senfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.senfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen nonfs; fs-nonfs)
reppres.district.diff.sennonfs.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.sennonfs.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.sennonfs.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen fs - non fs for fs noms)
reppres.district.diff.senfsnon.fs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.senfsnon.fs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.senfsnon.fs.lo=as.matrix(NA,nrow=length(seq.vote))

#difference (sen fs - non fs for nonfs noms)
reppres.district.diff.senfsnon.nonfs.mean=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.senfsnon.nonfs.hi=as.matrix(NA,nrow=length(seq.vote))
reppres.district.diff.senfsnon.nonfs.lo=as.matrix(NA,nrow=length(seq.vote))



for(i in 1:length(seq.vote)){
  #sen fs, fs judge 
  reppres.district.pred.senfs.fs=cbind(1,
                                       data.district_rep$Female,
                                       data.district_rep$ABANum,
                                       data.district_rep$EliteSchool,
                                       1, #fed soc judge,
                                       data.district_rep$SenatorDWNominate,
                                       data.district_rep$SenHomeStateSen,
                                       1, #fed soc senator
                                       data.district_rep$JCS,
                                       1, #interaction judge/senator
                                       data.district_rep[,9:180], #slope for Senator Unique ID
                                       data.district_rep[,181:199], #slope for Congress
                                       data.district_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.district.pred.senfs.fs=as.matrix(reppres.district.pred.senfs.fs)
  
  #sen fs, nonfs judge
  reppres.district.pred.senfs.nonfs=cbind(1,
                                          data.district_rep$Female,
                                          data.district_rep$ABANum,
                                          data.district_rep$EliteSchool,
                                          0, #fed soc judge
                                          data.district_rep$SenatorDWNominate,
                                          data.district_rep$SenHomeStateSen,
                                          1, #fed soc senator
                                          data.district_rep$JCS,
                                          0, #interaction judge/senator
                                          data.district_rep[,9:180], #slope for Senator Unique ID
                                          data.district_rep[,181:199], #slope for Congress
                                          data.district_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.district.pred.senfs.nonfs=as.matrix(reppres.district.pred.senfs.nonfs)
  
  #sen nonfs, fs judge
  reppres.district.pred.sennonfs.fs=cbind(1,
                                          data.district_rep$Female,
                                          data.district_rep$ABANum,
                                          data.district_rep$EliteSchool,
                                          1, #fed soc judge
                                          data.district_rep$SenatorDWNominate,
                                          data.district_rep$SenHomeStateSen,
                                          0, #fed soc senator
                                          data.district_rep$JCS,
                                          0, #interaction judge/senator
                                          data.district_rep[,9:180], #slope for Senator Unique ID
                                          data.district_rep[,181:199], #slope for Congress
                                          data.district_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.district.pred.sennonfs.fs=as.matrix(reppres.district.pred.sennonfs.fs)
  
  #sen nonfs, nonfs judge
  reppres.district.pred.sennonfs.nonfs=cbind(1,
                                             data.district_rep$Female,
                                             data.district_rep$ABANum,
                                             data.district_rep$EliteSchool,
                                             0, #fed soc judge
                                             data.district_rep$SenatorDWNominate,
                                             data.district_rep$SenHomeStateSen,
                                             0, #fed soc senator
                                             data.district_rep$JCS,
                                             0, #interaction judge/senator
                                             data.district_rep[,9:180], #slope for Senator Unique ID
                                             data.district_rep[,181:199], #slope for Congress
                                             data.district_rep[,200:204]) #slope for court level indicator (dichotomous here but need them for comformabe)
  reppres.district.pred.sennonfs.nonfs=as.matrix(reppres.district.pred.sennonfs.nonfs)
  
  
  
  
  #storage of predictions in next loop
  pp.reppres.district.senfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.district.senfs.nonfs=matrix(NA,nrow=n.draws)
  pp.reppres.district.sennonfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.district.sennonfs.nonfs=matrix(NA,nrow=n.draws)
  pp.reppres.district.diff.senfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.district.diff.sennonfs.fs=matrix(NA,nrow=n.draws)
  pp.reppres.district.diff.senfsnon.fs=matrix(NA,nrow=n.draws)
  pp.reppres.district.diff.senfsnon.nonfs=matrix(NA,nrow=n.draws)
  
  #prediction and probability loop
  for(j in 1:n.draws){
    #sen fs 
    xb.reppres.district.senfs.fs=plogis(reppres.district.pred.senfs.fs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.district.senfs.fs[j]=mean(xb.reppres.district.senfs.fs)
    
    xb.reppres.district.senfs.nonfs=plogis(reppres.district.pred.senfs.nonfs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.district.senfs.nonfs[j]=mean(xb.reppres.district.senfs.nonfs)
    
    
    #sen non fs
    xb.reppres.district.sennonfs.fs=plogis(reppres.district.pred.sennonfs.fs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.district.sennonfs.fs[j]=mean(xb.reppres.district.sennonfs.fs)
    
    xb.reppres.district.sennonfs.nonfs=plogis(reppres.district.pred.sennonfs.nonfs%*%t(sim.rep.reppres.fsint[j,]))
    pp.reppres.district.sennonfs.nonfs[j]=mean(xb.reppres.district.sennonfs.nonfs)
    
    #diff
    pp.reppres.district.diff.senfs.fs[j]=mean(xb.reppres.district.senfs.fs)-mean(xb.reppres.district.senfs.nonfs)
    pp.reppres.district.diff.sennonfs.fs[j]=mean(xb.reppres.district.sennonfs.fs)-mean(xb.reppres.district.sennonfs.nonfs)
    
    pp.reppres.district.diff.senfsnon.fs[j]=mean(xb.reppres.district.senfs.fs)-mean(xb.reppres.district.sennonfs.fs)
    pp.reppres.district.diff.senfsnon.nonfs[j]=mean(xb.reppres.district.senfs.nonfs)-mean(xb.reppres.district.sennonfs.nonfs)
  }
  #compute point estimates and CI for each
  reppres.district.senfs.fs.mean[i]=mean(pp.reppres.district.senfs.fs)
  reppres.district.senfs.fs.hi[i]=quantile(pp.reppres.district.senfs.fs,0.975)
  reppres.district.senfs.fs.lo[i]=quantile(pp.reppres.district.senfs.fs,0.025)
  
  reppres.district.senfs.nonfs.mean[i]=mean(pp.reppres.district.senfs.nonfs)
  reppres.district.senfs.nonfs.hi[i]=quantile(pp.reppres.district.senfs.nonfs,0.975)
  reppres.district.senfs.nonfs.lo[i]=quantile(pp.reppres.district.senfs.nonfs,0.025)
  
  reppres.district.sennonfs.fs.mean[i]=mean(pp.reppres.district.sennonfs.fs)
  reppres.district.sennonfs.fs.hi[i]=quantile(pp.reppres.district.sennonfs.fs,0.975)
  reppres.district.sennonfs.fs.lo[i]=quantile(pp.reppres.district.sennonfs.fs,0.025)
  
  reppres.district.sennonfs.nonfs.mean[i]=mean(pp.reppres.district.sennonfs.nonfs)
  reppres.district.sennonfs.nonfs.hi[i]=quantile(pp.reppres.district.sennonfs.nonfs,0.975)
  reppres.district.sennonfs.nonfs.lo[i]=quantile(pp.reppres.district.sennonfs.nonfs,0.025)
  
  reppres.district.diff.senfs.fs.mean[i]=mean(pp.reppres.district.diff.senfs.fs)
  reppres.district.diff.senfs.fs.hi[i]=quantile(pp.reppres.district.diff.senfs.fs,0.975)
  reppres.district.diff.senfs.fs.lo[i]=quantile(pp.reppres.district.diff.senfs.fs,0.025)
  
  reppres.district.diff.sennonfs.fs.mean[i]=mean(pp.reppres.district.diff.sennonfs.fs)
  reppres.district.diff.sennonfs.fs.hi[i]=quantile(pp.reppres.district.diff.sennonfs.fs,0.975)
  reppres.district.diff.sennonfs.fs.lo[i]=quantile(pp.reppres.district.diff.sennonfs.fs,0.025)
  
  reppres.district.diff.senfsnon.fs.mean[i]=mean(pp.reppres.district.diff.senfsnon.fs)
  reppres.district.diff.senfsnon.fs.hi[i]=quantile(pp.reppres.district.diff.senfsnon.fs,0.975)
  reppres.district.diff.senfsnon.fs.lo[i]=quantile(pp.reppres.district.diff.senfsnon.fs,0.025)
  
  reppres.district.diff.senfsnon.nonfs.mean[i]=mean(pp.reppres.district.diff.senfsnon.nonfs)
  reppres.district.diff.senfsnon.nonfs.hi[i]=quantile(pp.reppres.district.diff.senfsnon.nonfs,0.975)
  reppres.district.diff.senfsnon.nonfs.lo[i]=quantile(pp.reppres.district.diff.senfsnon.nonfs,0.025)
}






#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#
#Graphs
#
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################
#########################################################################################################################

#########################################################################
### Plot of Interaction (FedSoc Nom x Party) -- Figure X
#########################################################################
#png('InteractiveSuccess.png',pointsize=10,width=1400,height=960,res=300)
plot(NULL,ylim=c(0.45,1),xlim=c(0,9),main='',ylab='Likelihood of Senator Vote',xlab='',xaxt='n',cex.axis=0.8)
mtext(c('SCOTUS','CoA','District'),at=c(1.25,4.5,7.75),side=1,line=3)
axis(1,at=c(0.6,2.4,3.6,5.4,6.6,8.4),labels=c('Dem','Rep','Dem','Rep','Dem','Rep'),tick=T)
abline(v=3,lty=2)
abline(v=6,lty=2)
#dem scotus fs
points(0.45,scotus.dem.fs.mean[1],pch=16)
segments(0.45,scotus.dem.fs.hi,0.45,scotus.dem.fs.lo)
#dem scotus non
points(0.85,scotus.dem.nonfs.mean[1],pch=1)
segments(0.85,scotus.dem.nonfs.hi,0.85,scotus.dem.nonfs.lo)
#rep scotus fs
points(2.15,scotus.rep.fs.mean[1],pch=16)
segments(2.15,scotus.rep.fs.hi,2.15,scotus.rep.fs.lo)
#rep scotus nonfs
points(2.65,scotus.rep.nonfs.mean[1],pch=1)
segments(2.65,scotus.rep.nonfs.hi,2.65,scotus.rep.nonfs.lo)
#dem coa fs
points(3.45,coa.dem.fs.mean[1],pch=16)
segments(3.45,coa.dem.fs.hi,3.45,coa.dem.fs.lo)
#dem coa non
points(3.85,coa.dem.nonfs.mean[1],pch=1)
segments(3.85,coa.dem.nonfs.hi,3.85,coa.dem.nonfs.lo)
#rep coa fs
points(5.15,coa.rep.fs.mean[1],pch=16)
segments(5.15,coa.rep.fs.hi,5.15,coa.rep.fs.lo)
#rep coa nonfs
points(5.65,coa.rep.nonfs.mean[1],pch=1)
segments(5.65,coa.rep.nonfs.hi,5.65,coa.rep.nonfs.lo)
#dem district fs
points(6.45,district.dem.fs.mean[1],pch=16)
segments(6.45,district.dem.fs.hi,6.45,district.dem.fs.lo)
#dem district non
points(6.85,district.dem.nonfs.mean[1],pch=1)
segments(6.85,district.dem.nonfs.hi,6.85,district.dem.nonfs.lo)
#rep district fs
points(8.15,district.rep.fs.mean[1],pch=16)
segments(8.15,district.rep.fs.hi,8.15,district.rep.fs.lo)
#rep district nonfs
points(8.65,district.rep.nonfs.mean[1],pch=1)
segments(8.65,district.rep.nonfs.hi,8.65,district.rep.nonfs.lo)
#legend
legend(7.15,0.52,pch=c(1,16),c('No Affiliation','FedSoc Affiliate'),bty='n',y.intersp=0.75,cex=0.65)
#dev.off()








#####################################################
### Plot of interactions DW Nominate
#####################################################

###############
### Figure 
###############
# Scotus Dem
polygon.x.scotus.dem.fs.dwint.reppres=c(seq.vote.dw.dems.reppres,rev(seq.vote.dw.dems.reppres))
polygon.y.scotus.dem.fs.dwing.reppres=c(reppres.dw.scotus.dem.fs.hi,rev(reppres.dw.scotus.dem.fs.lo))
polygon.y.scotus.dem.nonfs.dwing.reppres=c(reppres.dw.scotus.dem.nonfs.hi,rev(reppres.dw.scotus.dem.nonfs.lo))

#png('DemNominateSCOTUSRepPres.png',pointsize=10,width=1400,height=960,res=300)
plot(seq.vote.dw.dems.reppres,reppres.dw.scotus.dem.fs.mean,type='l',ylim=c(0,0.85),ylab='Likelihood of Democratic Senator Vote',
     xlab='Senator NOMINATE Score',
     cex.axis=0.9)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.dem.fs.lo,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.dem.fs.hi,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.dem.nonfs.mean)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.dem.nonfs.lo,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.dem.nonfs.hi,lty=2)
polygon(x=polygon.x.scotus.dem.fs.dwint.reppres,y=polygon.y.scotus.dem.fs.dwing.reppres,col=adjustcolor('grey20',alpha.f=0.4))
polygon(x=polygon.x.scotus.dem.fs.dwint.reppres,y=polygon.y.scotus.dem.nonfs.dwing.reppres,col=adjustcolor('grey70',alpha.f=0.4))
legend(-0.025,0.175,pch=16,col=c('grey70','grey20'),c('Non-FS Nom','FS Nom'),bty='n',cex=0.65)
rug(jitter(noms_reppres$SenatorDWNominate))
#dev.off()


###############
### Figure 
###############

#first diff of scotus dem
#png('DemNominateSCOTUSFirstDiffsRepPres.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(seq.vote.dw.dems.reppres,reppres.dw.scotus.diff.dem.fs.mean,type='l',ylim=c(-0.45,0.15),ylab="First Difference (SCOTUS) \n FedSoc Affiliate - Non-Affiliate",xlab='Democratic Senator NOMINATE',
     cex.axis=0.8)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.diff.dem.fs.hi,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.scotus.diff.dem.fs.lo,lty=2)
abline(h=0,lty=2)
#dev.off()

###############
### Figure 
###############
# Scotus Rep
polygon.x.scotus.rep.fs.dwint.reppres=c(seq.vote.dw.reps.reppres,rev(seq.vote.dw.reps.reppres))
polygon.y.scotus.rep.fs.dwing.reppres=c(reppres.dw.scotus.rep.fs.hi,rev(reppres.dw.scotus.rep.fs.lo))
polygon.y.scotus.rep.nonfs.dwing.reppres=c(reppres.dw.scotus.rep.nonfs.hi,rev(reppres.dw.scotus.rep.nonfs.lo))

#png('RepNominateSCOTUSRepPres.png',pointsize=10,width=1400,height=960,res=300)
plot(seq.vote.dw.reps.reppres,reppres.dw.scotus.rep.fs.mean,type='l',ylim=c(0.75,1),ylab='Likelihood of Republican Senator Vote',
     xlab='Senator NOMINATE Score',
     cex.axis=0.8)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.rep.fs.lo,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.rep.fs.hi,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.rep.nonfs.mean)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.rep.nonfs.lo,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.rep.nonfs.hi,lty=2)
polygon(x=polygon.x.scotus.rep.fs.dwint.reppres,y=polygon.y.scotus.rep.fs.dwing.reppres,col=adjustcolor('grey20',alpha.f=0.4))
polygon(x=polygon.x.scotus.rep.fs.dwint.reppres,y=polygon.y.scotus.rep.nonfs.dwing.reppres,col=adjustcolor('grey70',alpha.f=0.4))
legend(0.715,0.8,pch=16,col=c('grey70','grey20'),c('Non-FS Nom','FS Nom'),bty='n',cex=0.65)
rug(jitter(noms_reppres$SenatorDWNominate))
#dev.off()


###############
### Figure 
###############
#first diff of scotus rep
#png('RepNominateSCOTUSFirstDiffsRepPres.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(seq.vote.dw.reps.reppres,reppres.dw.scotus.diff.rep.fs.mean,type='l',ylim=c(-0.1,0.125),ylab="First Difference (SCOTUS) \n FedSoc Affiliate - Non-Affiliate",xlab='Republican Senator NOMINATE',
     cex.axis=0.8)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.diff.rep.fs.hi,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.scotus.diff.rep.fs.lo,lty=2)
abline(h=0,lty=2)
#dev.off()




###############
### Figure 
###############
# coa Dem
polygon.x.coa.dem.fs.dwint.reppres=c(seq.vote.dw.dems.reppres,rev(seq.vote.dw.dems.reppres))
polygon.y.coa.dem.fs.dwing.reppres=c(reppres.dw.coa.dem.fs.hi,rev(reppres.dw.coa.dem.fs.lo))
polygon.y.coa.dem.nonfs.dwing.reppres=c(reppres.dw.coa.dem.nonfs.hi,rev(reppres.dw.coa.dem.nonfs.lo))

#png('DemNominateCoARepPres.png',pointsize=10,width=1400,height=960,res=300)
plot(seq.vote.dw.dems.reppres,reppres.dw.coa.dem.fs.mean,type='l',ylim=c(0,0.85),ylab='Likelihood of Democratic Senator Vote',
     xlab='Senator NOMINATE Score',
     cex.axis=0.9)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.dem.fs.lo,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.dem.fs.hi,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.dem.nonfs.mean)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.dem.nonfs.lo,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.dem.nonfs.hi,lty=2)
polygon(x=polygon.x.coa.dem.fs.dwint.reppres,y=polygon.y.coa.dem.fs.dwing.reppres,col=adjustcolor('grey20',alpha.f=0.4))
polygon(x=polygon.x.coa.dem.fs.dwint.reppres,y=polygon.y.coa.dem.nonfs.dwing.reppres,col=adjustcolor('grey70',alpha.f=0.4))
legend(-0.025,0.15,pch=16,col=c('grey70','grey20'),c('Non-FS Nom','FS Nom'),bty='n',cex=0.65)
rug(jitter(noms_reppres$SenatorDWNominate))
#dev.off()


###############
### Figure 
###############
#first diff of coa dem
#png('DemNominatecoaFirstDiffsRepPres.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(seq.vote.dw.dems.reppres,reppres.dw.coa.diff.dem.fs.mean,type='l',ylim=c(-0.5,0.1),ylab="First Difference (Circuit) \n FedSoc Affiliate - Non-Affiliate",xlab='Democratic Senator NOMINATE',
     cex.axis=0.7)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.diff.dem.fs.hi,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.coa.diff.dem.fs.lo,lty=2)
abline(h=0,lty=2)
#dev.off()


###############
### Figure 
###############
# coa Rep
polygon.x.coa.rep.fs.dwint.reppres=c(seq.vote.dw.reps.reppres,rev(seq.vote.dw.reps.reppres))
polygon.y.coa.rep.fs.dwing.reppres=c(reppres.dw.coa.rep.fs.hi,rev(reppres.dw.coa.rep.fs.lo))
polygon.y.coa.rep.nonfs.dwing.reppres=c(reppres.dw.coa.rep.nonfs.hi,rev(reppres.dw.coa.rep.nonfs.lo))

#png('RepNominateCoARepPres.png',pointsize=10,width=1400,height=960,res=300)
plot(seq.vote.dw.reps.reppres,reppres.dw.coa.rep.fs.mean,type='l',ylim=c(0.8,1),ylab='Likelihood of Republican Senator Vote',
     xlab='Senator NOMINATE Score',
     cex.axis=0.9)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.rep.fs.lo,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.rep.fs.hi,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.rep.nonfs.mean)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.rep.nonfs.lo,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.rep.nonfs.hi,lty=2)
polygon(x=polygon.x.coa.rep.fs.dwint.reppres,y=polygon.y.coa.rep.fs.dwing.reppres,col=adjustcolor('grey20',alpha.f=0.4))
polygon(x=polygon.x.coa.rep.fs.dwint.reppres,y=polygon.y.coa.rep.nonfs.dwing.reppres,col=adjustcolor('grey70',alpha.f=0.4))
legend(0.715,0.935,pch=16,col=c('grey70','grey20'),c('Non-FS Nom','FS Nom'),bty='n',cex=0.65)
rug(jitter(noms_reppres$SenatorDWNominate))
#dev.off()


###############
### Figure 
###############
#first diff of coa rep
#png('RepNominateCoAFirstDiffsRepPres.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(seq.vote.dw.reps.reppres,reppres.dw.coa.diff.rep.fs.mean,type='l',ylim=c(-0.025,0.055),ylab="First Difference (Circuit) \n FedSoc Affiliate - Non-Affiliate",xlab='Republican Senator NOMINATE',
     cex.axis=0.6)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.diff.rep.fs.hi,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.coa.diff.rep.fs.lo,lty=2)
abline(h=0,lty=2)
#dev.off()



###############
### Figure 
###############
# district Dem
polygon.x.district.dem.fs.dwint.reppres=c(seq.vote.dw.dems.reppres,rev(seq.vote.dw.dems.reppres))
polygon.y.district.dem.fs.dwing.reppres=c(reppres.dw.district.dem.fs.hi,rev(reppres.dw.district.dem.fs.lo))
polygon.y.district.dem.nonfs.dwing.reppres=c(reppres.dw.district.dem.nonfs.hi,rev(reppres.dw.district.dem.nonfs.lo))

#png('DemNominateDistrictRepPres.png',pointsize=10,width=1400,height=960,res=300)
plot(seq.vote.dw.dems.reppres,reppres.dw.district.dem.fs.mean,type='l',ylim=c(0.2,1),ylab='Likelihood of Democratic Senator Vote',
     xlab='Senator NOMINATE Score',
     cex.axis=0.9)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.dem.fs.lo,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.dem.fs.hi,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.dem.nonfs.mean)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.dem.nonfs.lo,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.dem.nonfs.hi,lty=2)
polygon(x=polygon.x.district.dem.fs.dwint.reppres,y=polygon.y.district.dem.fs.dwing.reppres,col=adjustcolor('grey20',alpha.f=0.4))
polygon(x=polygon.x.district.dem.fs.dwint.reppres,y=polygon.y.district.dem.nonfs.dwing.reppres,col=adjustcolor('grey70',alpha.f=0.4))
legend(-0.025,0.33,pch=16,col=c('grey70','grey20'),c('Non-FS Nom','FS Nom'),bty='n',cex=0.65)
rug(jitter(noms_reppres$SenatorDWNominate))
#dev.off()



###############
### Figure 
###############
#first diff of district dem
#png('DemNominatedistrictFirstDiffsRepPres.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(seq.vote.dw.dems.reppres,reppres.dw.district.diff.dem.fs.mean,type='l',ylim=c(-0.5,0.05),ylab="First Difference (District) \n FedSoc Affiliate - Non-Affiliate",xlab='Democratic Senator NOMINATE',
     cex.axis=0.8)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.diff.dem.fs.hi,lty=2)
lines(seq.vote.dw.dems.reppres,reppres.dw.district.diff.dem.fs.lo,lty=2)
abline(h=0,lty=2)
#dev.off()



###############
### Figure 
###############
# district Rep
polygon.x.district.rep.fs.dwint.reppres=c(seq.vote.dw.reps.reppres,rev(seq.vote.dw.reps.reppres))
polygon.y.district.rep.fs.dwing.reppres=c(reppres.dw.district.rep.fs.hi,rev(reppres.dw.district.rep.fs.lo))
polygon.y.district.rep.nonfs.dwing.reppres=c(reppres.dw.district.rep.nonfs.hi,rev(reppres.dw.district.rep.nonfs.lo))

#png('RepNominatedistrictRepPres.png',pointsize=10,width=1400,height=960,res=300)
plot(seq.vote.dw.reps.reppres,reppres.dw.district.rep.fs.mean,type='l',ylim=c(0.92,1),ylab='Likelihood of Republican Senator Vote',
     xlab='Senator NOMINATE Score',
     cex.axis=0.7)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.rep.fs.lo,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.rep.fs.hi,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.rep.nonfs.mean)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.rep.nonfs.lo,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.rep.nonfs.hi,lty=2)
polygon(x=polygon.x.district.rep.fs.dwint.reppres,y=polygon.y.district.rep.fs.dwing.reppres,col=adjustcolor('grey20',alpha.f=0.4))
polygon(x=polygon.x.district.rep.fs.dwint.reppres,y=polygon.y.district.rep.nonfs.dwing.reppres,col=adjustcolor('grey70',alpha.f=0.4))
legend(0.715,0.935,pch=16,col=c('grey70','grey20'),c('Non-FS Nom','FS Nom'),bty='n',cex=0.65)
rug(jitter(noms_reppres$SenatorDWNominate))
#dev.off()



###############
### Figure 
###############
#first diff of district rep
#png('RepNominateDistrictFirstDiffsRepPres.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(seq.vote.dw.reps.reppres,reppres.dw.district.diff.rep.fs.mean,type='l',ylim=c(-0.02,0.02),ylab="First Difference (District) \n FedSoc Affiliate - Non-Affiliate",xlab='Republican Senator NOMINATE',
     cex.axis=0.7)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.diff.rep.fs.hi,lty=2)
lines(seq.vote.dw.reps.reppres,reppres.dw.district.diff.rep.fs.lo,lty=2)
abline(h=0,lty=2)
#dev.off()





#########################################################################
### Plot of Interaction (FedSoc Nom x FedSoc Sen) -- Figure X
#########################################################################
#Plot of predictions for just republicans under rep prezzies (interaction with sen FedSoc affiliation)
#png('RepFedSocInteractiveSuccess.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL,ylim=c(0.8,1),xlim=c(0,9),main='',ylab='Likelihood of \n Republican Senator Vote',xlab='',xaxt='n')
mtext(c('SCOTUS','CoA','District'),at=c(1.25,4.5,7.75),side=1,line=3)
axis(1,at=c(0.6,2.4,3.6,5.4,6.6,8.4),labels=c('FS Sen','Non Sen','FS Sen','Non Sen','FS Sen','Non Sen'),tick=T,cex.axis=0.7)
abline(v=3,lty=2)
abline(v=6,lty=2)
#rep FS senator FS member
points(0.45,reppres.scotus.senfs.fs.mean[1],pch=16) 
segments(0.45,reppres.scotus.senfs.fs.hi,0.45,reppres.scotus.senfs.fs.lo)
#rep FS senator nonFS member
points(0.85,reppres.scotus.senfs.nonfs.mean[1],pch=1)
segments(0.85,reppres.scotus.senfs.nonfs.hi,0.85,reppres.scotus.senfs.nonfs.lo)
#rep senator FS member
points(2.15,reppres.scotus.sennonfs.fs.mean[1],pch=16) 
segments(2.15,reppres.scotus.sennonfs.fs.hi,2.15,reppres.scotus.sennonfs.fs.lo)
#rep senator nonFS member
points(2.65,reppres.scotus.sennonfs.nonfs.mean[1],pch=1)
segments(2.65,reppres.scotus.sennonfs.nonfs.hi,2.65,reppres.scotus.sennonfs.nonfs.lo)
#rep FS coa fs
points(3.45,reppres.coa.senfs.fs.mean[1],pch=16)
segments(3.45,reppres.coa.senfs.fs.hi,3.45,reppres.coa.senfs.fs.lo)
#rep FS coa non
points(3.85,reppres.coa.senfs.nonfs.mean[1],pch=1)
segments(3.85,reppres.coa.senfs.nonfs.hi,3.85,reppres.coa.senfs.nonfs.lo)
#rep coa fs
points(5.15,reppres.coa.sennonfs.fs.mean[1],pch=16)
segments(5.15,reppres.coa.sennonfs.fs.hi,5.15,reppres.coa.sennonfs.fs.lo)
#rep coa nonfs
points(5.65,reppres.coa.sennonfs.nonfs.mean[1],pch=1)
segments(5.65,reppres.coa.sennonfs.nonfs.hi,5.65,reppres.coa.sennonfs.nonfs.lo)
#rep FS district fs
points(6.45,reppres.district.senfs.fs.mean[1],pch=16)
segments(6.45,reppres.district.senfs.fs.hi,6.45,reppres.district.senfs.fs.lo)
#rep FS district non
points(6.85,reppres.district.senfs.nonfs.mean[1],pch=1)
segments(6.85,reppres.district.senfs.nonfs.hi,6.85,reppres.district.senfs.nonfs.lo)
#rep district fs
points(8.15,reppres.district.sennonfs.fs.mean[1],pch=16)
segments(8.15,reppres.district.sennonfs.fs.hi,8.15,reppres.district.sennonfs.fs.lo)
#rep district nonfs
points(8.65,reppres.district.sennonfs.nonfs.mean[1],pch=1)
segments(8.65,reppres.district.sennonfs.nonfs.hi,8.65,reppres.district.sennonfs.nonfs.lo)
#legend
legend(6.85,0.825,pch=c(1,16),c('No Affiliation','FedSoc Affiliate'),bty='n',y.intersp=0.75,cex=0.75)
#dev.off()







#Plot of First Diffs B/W FedSoc vs. NonFedSoc Rep Senators Only
#png('RepSenatorsFirstDiffFedSocAffiliation.png',pointsize=10,width=1400,height=960,res=300)
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL,ylim=c(0,0.1),xlim=c(0,9),main='',ylab='First Difference \n (Nominee: FedSoc-NonFedSoc)',xlab='',xaxt='n',cex.axis=0.8)
mtext(c('SCOTUS','CoA','District'),at=c(1.25,4.5,7.75),side=1,line=3)
axis(1,at=c(0.75,2.25,3.75,5.25,6.75,8.25),labels=c('FS Sen','Non Sen','FS Sen','Non Sen','FS Sen','Non Sen'),tick=T,cex.axis=0.8)
abline(v=3,lty=1)
abline(v=6,lty=1)
abline(h=0,lty=2)
#rep sen fs scotus
points(0.75,reppres.scotus.diff.senfs.fs.mean[1],pch=16)
segments(0.75,reppres.scotus.diff.senfs.fs.hi,0.75,reppres.scotus.diff.senfs.fs.lo)
#rep sen nonfs scotus
points(2.25,reppres.scotus.diff.sennonfs.fs.mean[1],pch=16)
segments(2.25,reppres.scotus.diff.sennonfs.fs.hi,2.25,reppres.scotus.diff.sennonfs.fs.lo)
#rep sen fs coa
points(3.75,reppres.coa.diff.senfs.fs.mean[1],pch=16)
segments(3.75,reppres.coa.diff.senfs.fs.hi,3.75,reppres.coa.diff.senfs.fs.lo)
#rep sen nonfs coa
points(5.25,reppres.coa.diff.sennonfs.fs.mean[1],pch=16)
segments(5.25,reppres.coa.diff.sennonfs.fs.hi,5.25,reppres.coa.diff.sennonfs.fs.lo)
#rep sen fs district
points(6.75,reppres.district.diff.senfs.fs.mean[1],pch=16)
segments(6.75,reppres.district.diff.senfs.fs.hi,6.75,reppres.district.diff.senfs.fs.lo)
#rep sen nonfs district
points(8.25,reppres.district.diff.sennonfs.fs.mean[1],pch=16)
segments(8.25,reppres.district.diff.sennonfs.fs.hi,8.25,reppres.district.diff.sennonfs.fs.lo)
#legend(6.85,-0.325,pch=c(1,16),c('No Affiliation','FedSoc Affiliate'),bty='n',y.intersp=0.75,cex=0.75)
#dev.off()












